Shivi
Shivi

Reputation: 1085

Filter the Property of object using LINQ

I have a scenario where i have to filter the Property of object using LINQ.

Edited: I have a Contract List dtCntList = DTIsland.GenericHelper.ConvertArrayListToGenericList(getList(), true);

Now in the Contract, I have various Property. Each contract have multiple Trips -> Trips have multiple flight. I have the scenario where i have to select all the flight number in a contract.

Like: Some Thing Like that

var allFlightNumber=from Cont in dtCntList[0] select dtCntList[0].trips.dtFlight

or have to select the Outbound trip

Var outBoundTrip = from cont in dtCntList[0] where cont.trip == OutBound

But i am not able to perform the LINQ on DTContract Object (dtCntList[0])

Please Help..

Upvotes: 0

Views: 1184

Answers (2)

Devendra D. Chavan
Devendra D. Chavan

Reputation: 9011

I assume you have the following class heirarchy,

class Contract
{
    public List<Trip> TripsList { get; set; }

    public Contract()
    {
        TripsList = new List<Trip>();
    }
}

class Trip
{
    public List<string> FlightNumbers { get; set; }

    public Trip()
    {
        FlightNumbers = new List<string>();
    }
}

First the builder method (to construct the object),

private Contract Build()
{
    Contract contract = new Contract();

    Trip trip = new Trip();
    trip.FlightNumbers.Add("101A");
    trip.FlightNumbers.Add("101B");
    trip.FlightNumbers.Add("101C");
    contract.TripsList.Add(trip);

    trip = new Trip();
    trip.FlightNumbers.Add("102A");
    trip.FlightNumbers.Add("102B");
    trip.FlightNumbers.Add("102C");
    contract.TripsList.Add(trip);

    return contract;
}

The the LINQ to find whether the flight number exists,

Contract c = Build();

if (c.TripsList.Where(trip => trip.FlightNumbers.Contains("103B")).Any())
{
    Console.WriteLine("Flight number 103B exists");
}

Upvotes: 1

Robert Giesecke
Robert Giesecke

Reputation: 4314

It is kinda hard to guess what you actually meant. These are two possible interpretations:

var result1 = from item in yourList
              where item.YourProperty=="SomeValue"
              select item;
var result2 = from property in yourObject.GetType().GetProperties()
              where property.Name =="SomeValue"
              select property;

Upvotes: 0

Related Questions