1

我有餐桌订单(姓名、日期、价格)

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

你能帮我吗,我必须改变什么才能得到我想要的输出,好吗?:)

4

1 回答 1

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)
  }
于 2012-05-03T19:14:25.677 回答