简体   繁体   English

Linq查询以在嵌套对象中查找对象

[英]Linq query to find objects in nested objects

Complete code is attached below. 完整的代码附在下面。 I want to find all Cars objects that has a certain color. 我想找到所有具有特定颜色的Cars对象。

The Vehicles list consists of two lists of type Cars. 车辆列表由两个类型的汽车列表组成。 The Cars lists consist of objects of type Colours. “汽车”列表由“颜色”类型的对象组成。

Instead of using foreach iterations, what would a Linq query look like? 而不是使用foreach迭代,Linq查询将是什么样?

using System.Collections.Generic;

public class Test
{
    public void testitall()
    {
        List<Cars> test = FindCarByColour("Red");
    }

    /// <summary>
    /// Find all cars with property ColourName like colourcriteria
    /// </summary>
    /// <param name="colourcriteria"></param>
    /// <returns></returns>
    private List<Cars> FindCarByColour(string colourcriteria)
    {
     // Populate data classes
        Colours Col1 = new Colours();
        Col1.ColourName ="Red";
        Colours Col2 = new Colours();
        Col2.ColourName ="Blue";
        List<Cars> CarList1 = new List<Cars>();
        CarList1.Add(new Cars { Name = "Saab", ColourProperties = Col1 });
        CarList1.Add(new Cars { Name = "Citroen", ColourProperties = Col2});
        List<Cars> CarList2 = new List<Cars>();
        CarList2.Add(new Cars { Name = "Daf", ColourProperties = Col1 });
        CarList2.Add(new Cars { Name = "Vauxhall", ColourProperties = Col2 });
        List<Vehicles> vehicleList = new List<Vehicles>();
        vehicleList.Add(new Vehicles { Vechicle = "SmallCar", Cars = CarList1 });
        vehicleList.Add(new Vehicles { Vechicle = "MediumCar", Cars = CarList2 });

        // Search 
        List<Cars> ListOfFindings = new List<Cars>();
        foreach (Vehicles vehicleItem in vehicleList)
        {
            foreach (Cars caritem in vehicleItem.Cars)
            {
                if (caritem.Name != null && caritem.ColourProperties.ColourName == colourcriteria)
                {
                    ListOfFindings.Add(caritem);
                }
            }
        }
        return ListOfFindings;
    }

    // Data classes
    public class Vehicles
    {
        public string Vechicle { get; set; }
        public List<Cars> Cars { get; set; }
    }
    public class Cars
    {
        public string Name { get; set; }
        public Colours ColourProperties { get; set; }
    }
    public class Colours
    {
        public string ColourName { get; set; }
    }

} }

You could probably use something like: 您可能会使用类似:

var listOfFindings = (from vehicleItem in vehicleList
                      from carItem in vehicleItem.Cars
                      where carItem.Name != null
                      && carItem.ColourProperties.ColourName == colourcriteria
                      select carItem).ToList();

or 要么

var listOfFindings = vehicleList.SelectMany(vehicleItem => vehicleItem.Cars).Where(carItem => carItem.Name != null && carItem.ColourProperties.ColourName == colourcriteria).ToList();

Depending on what style of Linq you want to use. 根据您要使用的Linq样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM