8
select description, min(date), max(date), sum(value1), sum(value2) from table
where description = 'Axxx' and filter = 'L'
group by description

如何使用 Linq / C# 执行此查询?

4

3 回答 3

11

未经测试,但以下应该可以工作:

table
   .Where(x=>x.description=="Axxx" && x.filter=="L")
   .GroupBy(x=>x.description)
   .Select(x=>new {
       description=x.Key,
       mindate=x.Min(z=>z.date),
       maxdate=x.Max(z=>z.date),
       sumvalue1=x.Sum(z=>z.value1),
       sumvalue2=x.Sum(z=>z.value2)
    });
于 2013-11-12T13:20:21.103 回答
1

像这样的东西应该工作。未测试

 var q = from b in table
            group b by b.Owner into g
            where description = 'Axxx'
            select new
                       {
                           description = g.description ,
                           date = g.min(),
                           date = g.max(),
                           value1= g.Sum(item => item.value1),
                           value2= g.Sum(item => item.value2),
                       };
于 2013-11-12T13:27:55.823 回答
0

那应该工作

var q = from s in db.table
            where description = 'Axxx' and filter = 'L'
            group s by s.description into g
            select new {YourDescription = g.description, MaxDate = g.Max(s => s.date) }

这个答案也可能有帮助..在这里

于 2013-11-12T13:25:45.660 回答