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

时间:2012-05-03 19:04:03

标签: c# .net linq linq-to-objects

我有表格订单(姓名,日期,价格)

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

我想在星期一出示订单名称和价格总和。 输出我想要的东西:

  horse, 100
  cat, 50
  dog, 0

但现在我只有这个:

  horse, 100
  cat, 50

使用此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)
  }

你能帮助我吗,我必须改变什么来获得我想要的输出,拜托? :)

1 个答案:

答案 0 :(得分:8)

首先尝试灌浆,然后对星期一进行过滤

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)
  }
相关问题