C#LINQ计算Group By

时间:2010-06-24 16:30:05

标签: c# linq sql-order-by group-by

要把它归结为黄铜钉,我有一个包含100种不同记录的列表。汽车有年,制造和模型。

我需要能够:

  1. 按照年份订购,但我需要:

  2. 拥有所有福特汽车     探险家作为一个“团体”出现在     我的清单结束(也订购了     根据年份)

  3. 我不确定我是否必须创建2个单独的List然后合并它们......似乎应该有一个更好的方法来做到这一点,而不是必须创建多个对象。

    感谢您的帮助!

    非常感谢, 保罗

    示例:

    2001悍马H1
    2002福特福克斯2008 2008宝马325i
    2008福特Escape
    2009 BMW 328i
    2009 2009三菱Galant < br /> 2003年福特资源管理器
    2004年福特资产管理器
    2008福特资产管理器
    2009福特资产管理器

    车轮

3 个答案:

答案 0 :(得分:3)

如果您真的只想将福特探险家分组,您可以执行以下操作:

var groupedByFordExplorer = from c in cars
                    let isFordExplorer = c.Make == "Ford" && c.Model == "Explorer"
                    orderby isFordExplorer, c.Year
                    select c;

上述查询的作用是创建一个内联值isFordExplorer,并使用let关键字为其指定一个布尔值,指示该车是否为福特资源管理器。然后可以将该值与年份一起排序。以下是一个应该展示这个概念的工作程序:

class Program
{
    static void Main(string[] args)
    {
        var cars = new List<Car>
        {
            new Car { Year = 2009, Make = "Ford", Model = "Explorer" },
            new Car { Year = 2001, Make = "Hummer", Model = "H1" },             
            new Car { Year = 2002, Make = "Ford", Model = "Focus" },
            new Car { Year = 2008, Make = "BMW", Model = "325i" },
            new Car { Year = 2008, Make = "Ford", Model = "Explorer" },             
            new Car { Year = 2008, Make = "Ford", Model = "Escape" },               
            new Car { Year = 2009, Make = "Mitsubishi", Model = "Galant" },
            new Car { Year = 2004, Make = "Ford", Model = "Explorer" },
            new Car { Year = 2009, Make = "BMW", Model = "329i" },
            new Car { Year = 2003, Make = "Ford", Model = "Explorer" }              
        };

        var groupedByFordExplorer = from c in cars
                                    let isFordExplorer = c.Make == "Ford" && c.Model == "Explorer"
                                    orderby isFordExplorer, c.Year
                                    select c;

        foreach (var car in groupedByFordExplorer)
        {
            Console.WriteLine("{0} {1} {2}", car.Year, car.Make, car.Model);
        }

        Console.ReadLine();
    }
}

class Car
{
    public int Year { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

答案 1 :(得分:2)

这应该这样做:

var query = list.OrderBy(car => car.Make == "Ford" && car.Model == "Explorer")
                .ThenBy(car => car.Year);

(显然调整了对福特Explorer的测试。)

基本上将Ford Explorer-ness视为布尔属性,其中false在true之前排序。当然,您也可以将其表达为查询表达式:

var query = from car in list
            orderby car.Make == "Ford" && car.Model == "Explorer", car.Year
            select car;

(在这种情况下,我倾向于使用点符号。)

答案 2 :(得分:0)

cars.OrderBy(c => 
        (c.Make == "Ford" && c.Model == "Explorer")
        ? c.Year + 10000
        : c.Year)
    .ToList()

考虑下面我的评论,如果是LINQ to sql,我可能会用联盟写一下:

cars.Where(c => c.Make != "Ford" || c.Model != "Explorer")
    .OrderBy(c => c.Year)
    .Union(cars.Where(c => c.Make == "Ford" && c.Model == "Explorer")
               .OrderBy(c => c.Year))