Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在EF中选择按日期分组的多个计数_C#_Entity Framework_Count_Linq Group - Fatal编程技术网

C# 如何在EF中选择按日期分组的多个计数

C# 如何在EF中选择按日期分组的多个计数,c#,entity-framework,count,linq-group,C#,Entity Framework,Count,Linq Group,我有一张大致如下的桌子: Date | Category | Name 01/02/2014 | A | Foo 01/02/2014 | B | Bar 02/02/2014 | A | Baz 02/02/2014 | A | Bla Date | CategoryACount | CategoryBCount 01/02/2014 | 1 | 1 0

我有一张大致如下的桌子:

Date        | Category  | Name
01/02/2014  |    A      | Foo
01/02/2014  |    B      | Bar
02/02/2014  |    A      | Baz
02/02/2014  |    A      | Bla
Date        | CategoryACount | CategoryBCount
01/02/2014  |       1        |      1
02/02/2014  |       2        |      0
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(),
                  })
我正在尝试构建一个查询,生成如下内容:

Date        | Category  | Name
01/02/2014  |    A      | Foo
01/02/2014  |    B      | Bar
02/02/2014  |    A      | Baz
02/02/2014  |    A      | Bla
Date        | CategoryACount | CategoryBCount
01/02/2014  |       1        |      1
02/02/2014  |       2        |      0
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(),
                  })
我现在有一个存储过程,它循环遍历日期,并通过逐个查询数据创建一个临时表,我们计划将所有应用程序逻辑移出存储过程


如何在EF中生成此查询?

如果您的表如下所示

public class Table
{
   public DateTime Date { get; set; }
   public string  Category { get; set; }
   public string Name { get; set; }
}
您可以使用这样的qry:

Date        | Category  | Name
01/02/2014  |    A      | Foo
01/02/2014  |    B      | Bar
02/02/2014  |    A      | Baz
02/02/2014  |    A      | Bla
Date        | CategoryACount | CategoryBCount
01/02/2014  |       1        |      1
02/02/2014  |       2        |      0
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();
var lst=newlist();
lst.Add(new Table(){Date=new DateTime(2014,2,1),Category=“A”});
lst.Add(newtable(){Date=newdatetime(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)
.选择(c=>new{
日期=c键,
CatA=c.Where(q=>q.Category==“A”).Count(),
CatB=c.Where(q=>q.Category==“B”).Count(),
})
.Dump();

类别的数量是否不同,或者您只有两个类别
A
B
?如果是第一个,像这样的项目的可枚举性:
{Category,Count}
是否符合您的需要?类别的数量是固定的,正如预期的那样,使用简洁易懂的linq表达式。谢谢@那个家伙——没问题:)