如果你的桌子看起来像这样
public class Table
{
public DateTime Date { get; set; }
public string Category { get; set; }
public string Name { get; set; }
}
你可以使用这样的 qry:
db.Table.GroupBy(c => c.Date)
.Select(c => new
{
Date = c.Key,
CatA = c.Where(q => q.Category == "A").Count(),
CatB = c.Where(q => q.Category == "B").Count(),
})
要测试它 - 使用 LinqPad 并运行它:
var lst = new List<Table>();
lst.Add(new Table(){Date = new DateTime(2014,2,1),Category = "A"});
lst.Add(new Table(){Date = new DateTime(2014,2,1),Category = "B"});
lst.Add(new Table(){Date = new DateTime(2014,2,2),Category = "A"});
lst.Add(new Table(){Date = new DateTime(2014,2,2),Category = "A"});
lst.GroupBy(c => c.Date)
.Select(c => new {
Date = c.Key,
CatA = c.Where(q => q.Category == "A").Count(),
CatB = c.Where(q => q.Category == "B").Count(),
})
.Dump();
