Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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# 由“etc”组成的“x”组做什么?_C#_Sql - Fatal编程技术网

C# 由“etc”组成的“x”组做什么?

C# 由“etc”组成的“x”组做什么?,c#,sql,C#,Sql,虽然我在MSDN和许多书中搜索过,但我找不到任何关于这种查询的帮助。也许这里有人能帮我。下面是一个例子: var query = from document in Database.Documents group document.OwnerID by new {

虽然我在MSDN和许多书中搜索过,但我找不到任何关于这种查询的帮助。也许这里有人能帮我。下面是一个例子:

 var query = from document in Database.Documents                           
                       group document.OwnerID by 
                       new 
                       { 
                           OwnerName= document.Owner.OwnerName,
                           ReleaseDate= document.ReleaseDate,
                           OwnerID= document.OwnerID,
                           Status= document.StatusId

                       }
                       into grupo                               
                       select new AmountOfDocuments
                       {
                           OwnerName= grupo.Key.OwnerName,
                           ReleaseDate= grupo.Key.ReleaseDate,
                           OwnerID= grupo.Key.OwnerID,
                           UDocuments= grupo.Count(),
                           SDocuments= grupo.Count()
                       };
我需要让UDocuments和SDocuments返回状态分别为U或S的文档数。对此的帮助将不胜感激,但我需要弄清楚查询本身是如何工作的


提前谢谢

Lambda表达式是你的朋友

UDocuments = grupo.Where(x => x.Status == "U").Count(),
SDocuments = grupo.Where(x => x.Status == "S").Count()
编辑:不需要输入where

UDocuments = grupo.Count(x => x.Status == "U"),
SDocuments = grupo.Count(x => x.Status == "S")

检查在Linqpad中创建的以下代码,它以两种不同的方式完成任务并产生相同的结果。第二种方法更可取,因为它将状态作为分组值进行预测,因此计数变得更简单

void Main()
{
    var testList = Test.Create();

    var result = 
    testList.GroupBy(x=>new {
                          x.OwnerName,
                          x.OwnerId,
                          x.ReleaseDates
                            })
            .Select(y=>new {
                           y.Key.OwnerName,
                           y.Key.OwnerId,
                           statusU = y.Count(m=>m.status == "U"),
                           statusS = y.Count(m=>m.status == "S"),
                            });

    result.Dump();

    var result1 = 
    testList.GroupBy(x=>new {
                          x.OwnerName,
                          x.OwnerId,
                          x.ReleaseDates
                            },x=>x.status)
            .Select(y=>new {
                           y.Key.OwnerName,
                           y.Key.OwnerId,
                           statusU = y.Count(m=>m=="U"),
                           statusS = y.Count(m=>m=="S"),
                            });

    result1.Dump();
}

public class Test
{
    public string OwnerName {get; set;}

    public int OwnerId {get; set;}

    public string status {get; set;}

    public DateTime ReleaseDates{get; set;}

    public static List<Test> Create()
    {
      return new List<Test>()
        {

        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "S",
          ReleaseDates = DateTime.Now
        },
        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "S",
          ReleaseDates = DateTime.Now
        },
        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "S",
          ReleaseDates = DateTime.Now
        },
        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "U",
          ReleaseDates = DateTime.Now
        },
        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "U",
          ReleaseDates = DateTime.Now
        },
        new Test
        {
          OwnerName = "ABCD",
          OwnerId = 1,
          status = "S",
          ReleaseDates = DateTime.Now
        }

        };
    }   
}

您的意思是==not=并且Count接受一个谓词,因此更简单地说,grupo.Countx=>x.Status==Ux是作为int变量生成的,而不是作为AmountOfDocuments one.x不能是整数,除非您投影一个整数值,否则请检查该部分,在您的解决方案中,它应该是complete对象。查看如何在由创建的解决方案中进行项目me@StevenEccles您已确认但未更正其中的版本,请将其应具有的更正设置为==,当前格式为错误