Linq c#where条件列表

时间:2016-03-23 20:36:26

标签: c# linq

我有简单的班级模型Car,有两个字段

public class Car
{
    public string CarBrand { get; set; }
    public List<CarModel> carModel;         
}

public class CarModel
{
    public string Name { get; set; }
    public string YearOfProduction{ get; set; }
}

现在,我的问题是如何获得新的汽车清单列表,其中YearOfProduction仅为2016年。

3 个答案:

答案 0 :(得分:3)

如果您需要车辆列表,其中至少有一个车型属于特定年份,您可以这样做:

var temp = cars.Where(c=>c.CarModels.Any(cm=>cm.YearOfProduction== Year)).ToList();

如果所有车型必须属于该年,则使用All扩展方法代替Any

var temp = cars.Where(c=>c.CarModels.All(cm=>cm.YearOfProduction== Year)).ToList();

答案 1 :(得分:0)

您正在寻找Any功能:

cars.Where(c => c.carModel.Any(cm => cm.YearOfProduction == 2016));

答案 2 :(得分:0)

您可以对此查询使用Exists方法。请检查以下示例;

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Car
    {
        public string CarBrand { get; set; }
        public List<CarModel> carModel;
    }

    public class CarModel
    {
        public string Name { get; set; }
        public string YearOfProduction { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<Car>();
            list.Add(
                new Car
                    {
                        CarBrand = "Ford",
                        carModel = new List<CarModel>{ 
                            new CarModel{Name="Fiesta", YearOfProduction="2010"},
                            new CarModel{Name="Fiesta", YearOfProduction="2011"},
                            new CarModel{Name="Fiesta", YearOfProduction="2012"},
                            new CarModel{Name="Fiesta", YearOfProduction="2013"},
                            new CarModel{Name="Fiesta", YearOfProduction="2014"},
                            new CarModel{Name="Fiesta", YearOfProduction="2015"},
                            new CarModel{Name="Fiesta", YearOfProduction="2016"},
                            new CarModel{Name="Mondeo", YearOfProduction="2010"},
                            new CarModel{Name="Mondeo", YearOfProduction="2011"},
                            new CarModel{Name="Mondeo", YearOfProduction="2012"},
                            new CarModel{Name="Mondeo", YearOfProduction="2013"},
                            new CarModel{Name="Mondeo", YearOfProduction="2014"},
                            new CarModel{Name="Mondeo", YearOfProduction="2015"},
                            new CarModel{Name="Mondeo", YearOfProduction="2016"},
                            new CarModel{Name="Focus", YearOfProduction="2010"},
                            new CarModel{Name="Focus", YearOfProduction="2011"},
                            new CarModel{Name="Focus", YearOfProduction="2012"},
                            new CarModel{Name="Focus", YearOfProduction="2013"},
                            new CarModel{Name="Focus", YearOfProduction="2014"},
                            new CarModel{Name="Focus", YearOfProduction="2015"},

                        }
                    }

                    );
            list.Add(new Car
                    {
                        CarBrand = "Volkswagen",
                        carModel = new List<CarModel>{ 
                            new CarModel{Name="Polo", YearOfProduction="2010"},
                            new CarModel{Name="Polo", YearOfProduction="2011"},
                            new CarModel{Name="Polo", YearOfProduction="2012"},
                            new CarModel{Name="Polo", YearOfProduction="2013"},
                            new CarModel{Name="Polo", YearOfProduction="2014"},
                            new CarModel{Name="Polo", YearOfProduction="2015"},
                            new CarModel{Name="Golf", YearOfProduction="2010"},
                            new CarModel{Name="Golf", YearOfProduction="2011"},
                            new CarModel{Name="Golf", YearOfProduction="2012"},
                            new CarModel{Name="Golf", YearOfProduction="2013"},
                            new CarModel{Name="Golf", YearOfProduction="2014"},
                            new CarModel{Name="Golf", YearOfProduction="2015"},
                            new CarModel{Name="Passat", YearOfProduction="2010"},
                            new CarModel{Name="Passat", YearOfProduction="2011"},
                            new CarModel{Name="Passat", YearOfProduction="2012"},
                            new CarModel{Name="Passat", YearOfProduction="2013"},
                            new CarModel{Name="Passat", YearOfProduction="2014"},
                            new CarModel{Name="Passat", YearOfProduction="2015"},

                        }
                    });

            var newCars = list.Where(s => s.carModel.Exists(m => m.YearOfProduction == "2016"));

            foreach (var brand in newCars)
            {
                foreach (var model in brand.carModel)
                {
                    Console.WriteLine("Brand:{0}, Model:{1}, YearOfPrd:{2}", brand.CarBrand, model.Name, model.YearOfProduction);

                }
            }
            Console.ReadLine();
        }
    }
}