简体   繁体   English

Linq:按名称选择所有项目,计数等于0

[英]Linq: Select all items group by name with count equal 0

I have table orders(name, date, price) 我有餐桌订单(名称,日期,价格)

  horse, 7.5.2012, 100
  cat, 14.5.2012, 50
  horse, 8.5.2012, 70,
  dog, 13.5.2012, 40

and I want to show up orders name and sum of price on Monday. 我想在星期一显示订单名称和价格总和。 Output what I want: 输出我想要的:

  horse, 100
  cat, 50
  dog, 0

but now I have only this: 但是现在我只有这个:

  horse, 100
  cat, 50

with this linq query 这个LINQ查询

  from c in orders
  where (int)c.date.DayOfWeek == this._monday
  group c by c.name into g
  select new {
    Name = g.Key,
    Price = g.Sum(c => c.Price)
  }

Can you help me, what I must change to get output what I want, please? 您能帮我吗,我必须改变什么才能获得想要的输出? :) :)

Try groupping first then do the filtering for Monday 首先尝试分组,然后在星期一进行过滤

from c in orders  
group c by c.name into g
  select new {
    Name = g.Key,
    Price = g.Where(c => (int)c.date.DayOfWeek == this._monday).Sum(c => c.Price)
  }

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

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