Kiran Shahi
Kiran Shahi

Reputation: 7980

How to select object and its properties base on its properties which is list with linq?

I have an object which has a property which is list. Based on that list I want to filter property of it along with other properties of an object.

I have following object

{
    Adult: 1,
    Child: 0,
    Infant: 0,
    Origin: 'ABC',
    Destination: 'XYZ',
    DomesticFlightInfos: [{
            FlightID: flightId1,
            FareID: fareId1,
            IsInbound: true
        },
        {
            FlightID: flightId2,
            FareID: fareId2,
            IsInbound: false
        }
    ]
};

I want following two objects

{
    Adult: 1,
    Child: 0,
    Infant: 0,
    Origin: 'ABC',
    Destination: 'XYZ',
    DomesticFlightInfos: [{
        FlightID: flightId1,
        FareID: fareId1,
        IsInbound: true
    }]
};

Adult: 1,
    Child: 0,
    Infant: 0,
    Origin: 'ABC',
    Destination: 'XYZ',
    DomesticFlightInfos: [{
        FlightID: flightId2,
        FareID: fareId2,
        IsInbound: false
    }]

I was trying to do reservation.DomesticFlightInfos.Where(x => !x.IsInbound); which was wrong and got casting issue. What could be the best solution for it?

C# Object

using System;
using System.Collections.Generic;                   
public class Program
{
    public static void Main()
    {
        new DomesticFlightReservation()

        {
            Adult = 1,
            Child = 0,
            Infant = 0,
            Origin = "ABC",
            Destination = "XYZ",
            DomesticFlightInfos = new List<DomesticFlightInfo>(){
                new DomesticFlightInfo{
        FlightID= "flightId1",
            FareID= "fareId1",
            IsInbound= true
                },
           new DomesticFlightInfo {
                FlightID= "flightId2",
                FareID= "fareId2",
                IsInbound= false
            }
        }
        };
    }
}

public class DomesticFlightReservation
{
    public int Adult { get; set; } = 0;
    public int Child { get; set; } = 0;
    public int Infant { get; set; } = 0;
    public string Origin { get; set; }
    public string Destination { get; set; }
    public List<DomesticFlightInfo> DomesticFlightInfos { get; set; }
}
public class DomesticFlightInfo
{
    public string FlightID { get; set; }
    public string FareID { get; set; }
    // True for inbound, false for outbound
    public bool IsInbound { get; set; }
}

Upvotes: 1

Views: 114

Answers (2)

Anu Viswan
Anu Viswan

Reputation: 18155

Since you haven't provided C# object, assuming your C# objects to be defined as

public class DomesticFlightInfo
{
    public string FlightID { get; set; }
    public string FareID { get; set; }
    public bool IsInbound { get; set; }
}

public class RootObject
{
    public int Adult { get; set; }
    public int Child { get; set; }
    public int Infant { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public List<DomesticFlightInfo> DomesticFlightInfos { get; set; }
}

If your intention is to separate the Inbounds and Outbound flights to separate groups, you could

var reservation = JsonConvert.DeserializeObject<RootObject>(str);
var flattenedReservation = new[]{reservation}.SelectMany(x=>x.DomesticFlightInfos
                              .GroupBy(v=>v.IsInbound)
                              .Select(c =>
                                        {
                                           x.DomesticFlightInfos = c.ToList(); 
                                           return x;
                                         }));
var jsonCollection = flattenedReservation.Select(x=> JsonConvert.SerializeObject(x,settings));

If your intention is to separate out each flight info into separate jsons, you could

var flattenedReservation = new[]{reservation}.SelectMany(x=>x.DomesticFlightInfos
                              .Select(c =>
                                   {
                                     x.DomesticFlightInfos = new List<DomesticFlightInfo>{c}; 
                                     return x;
                                   }));
var jsonCollection = flattenedReservation.Select(x=> JsonConvert.SerializeObject(x));

Output

enter image description here

Upvotes: 1

Gauravsa
Gauravsa

Reputation: 6524

If you have a list as a collection, you can use SelectMany

So, if you have a list

List<RootObject> rootObjects

then you can do this like flatten the list to get individual items:

rootObjects.SelectMany(x=> x.DomesticFlightInfos).Where(y => !y.IsInbound);

Upvotes: 1

Related Questions