使用LINQ过滤对象的属性

时间:2021-07-31 02:24:18

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

我有一个场景,我必须使用LINQ过滤对象的属性。

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

编辑:我有一个合同列表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

var allFlightNumber=来自dtCntList[0]中的Cont,选择dtCntList[0].trips.dtFlight

or have to select the Outbound trip

或者必须选择出站旅行

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

Var outBoundTrip =来自dtCntList[0]中的cont,其中context .trip =出站

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

但是我不能在DTContract对象上执行LINQ (dtCntList[0])

Please Help..

请帮助. .

2 个解决方案

#1


1  

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,

查找航班号是否存在的LINQ,

Contract c = Build();

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

#2


0  

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;

#1


1  

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,

查找航班号是否存在的LINQ,

Contract c = Build();

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

#2


0  

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;