简体   繁体   English

选择Min和Max LINQ

[英]Select Min and Max LINQ

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

How to perform this query using Linq / C#? 如何使用Linq / C#执行此查询?

Not tested, but the following should work: 未经测试,但以下情况应该有效:

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)
    });

That should work 这应该工作

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) }

That answer may help, too .. here 这个答案也许有帮助.. 这里

something like this should work. 这样的事情应该有效。 not tested 未经测试

 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),
                       };

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

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