特定区域的最大和最小费率-lambda查询

时间:2013-04-15 18:07:04

标签: linq entity-framework

my table looks like-
  Area    MaxRate  MinRate Quarter year
    crossing  1000     800     1       2013
    crossing  900      700     2       2013
    crossing  800      600     3       2013
crossing 700 500 4 2013 crossing 600 500 1 2012 crossing 550 450 2 2012 crossing 500 400 3 2012
crossing 450 350 4 2012 indroper 2000 1500 1 2013 indroper 1800 1500 2 2013

 I want max and min rates for the particular area in current year as -

 **Area    MaxRate  MinRate       year**
 crossing  1000     500           2013

请帮我在LINQ-lambda表示法

中查找查询

1 个答案:

答案 0 :(得分:2)

您可以使用GroupBy执行此操作:

var results = table.GroupBy(row => new { Area = row.Area, Year = row.year })
                   .Select(g => new { 
                                       Area = g.Key.Area, 
                                       MaxRate = g.Max(i => i.MaxRate),
                                       MinRate = g.Min(i => i.MinRate),
                                       Year = g.Key.Year
                                    });