Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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# C的微复杂linq#_C#_Sql_Linq - Fatal编程技术网

C# C的微复杂linq#

C# C的微复杂linq#,c#,sql,linq,C#,Sql,Linq,我之前问了一个关于检索服务器端数据计数的问题,并在站点上得到了解决方案。建议使用linq,它工作得很好,但是因为我对它比较陌生,所以我需要一些深入的帮助 使用John的解决方案: class Guy { public int age; public string name; public Guy( int age, string name ) { this.age = age; this.name = name; } } class P

我之前问了一个关于检索服务器端数据计数的问题,并在站点上得到了解决方案。建议使用linq,它工作得很好,但是因为我对它比较陌生,所以我需要一些深入的帮助

使用John的解决方案:

class Guy
{
    public int age; public string name;
    public Guy( int age, string name ) {
        this.age = age;
        this.name = name;
    }

}

class Program
{
    static void Main( string[] args ) {
        var GuyArray = new Guy[] { 
        new Guy(22,"John"),new Guy(25,"John"),new Guy(27,"John"),new Guy(29,"John"),new Guy(12,"Jack"),new Guy(32,"Jack"),new Guy(52,"Jack"),new Guy(100,"Abe")};

    var peeps = from f in GuyArray group f by f.name into g select new { name = g.Key, count = g.Count() };

        foreach ( var record in peeps ) {
            Console.WriteLine( record.name + " : " + record.count );
        }

    }
}
我可以按照John的建议,使用上面的方法来计算John、Jake和Abe的出现次数。但是,如果问题稍微复杂一点呢

var GuyArray = new Guy[] { 
new Guy(22,"John", "happy"),new Guy(25,"John", "sad"),new Guy(27,"John", "ok"),
new Guy(29,"John", "happy"),new Guy(12,"Jack", "happy"),new Guy(32,"Jack", "happy"),
new Guy(52,"Jack", "happy"),new Guy(100,"Abe", "ok")};

上面的代码可以很好地检索不同名字的出现次数,但是如果我需要这些名字的出现次数,以及每个快乐、悲伤或正常的人的出现次数,该怎么办。ie输出是:姓名,姓名数,快乐姓名数,悲伤姓名数,正常姓名数。如果linq不是解决这一问题的最佳方案,我准备听取所有备选方案。非常感谢您的帮助。

坦率地说,不清楚您想要的是快乐的总人数,还是快乐的总人数(也可以是悲伤的,ok)。我会给你一个解决方案,可以给你们两个

var nameGroups = from guy in GuyArray 
                 group guy by guy.name into g
                 select new { 
                     name = g.Key,
                     count = g.Count(),
                     happy = g.Count(x => x.status == "happy"),
                     sad = g.Count(x => x.status == "sad"),
                     ok = g.Count(x => x.status == "ok")
                 };
然后:

如果你想要总的快乐,悲伤,ok,你可以说:

Console.WriteLine(nameGroups.Sum(nameGroup => nameGroup.happy));
等等

此外,您应该创建一个枚举

public enum Mood {
    Happy,
    Sad,
    Okay
}
然后

class Guy {
    public int Age { get; set; }
    public string Name { get; set; }
    public Mood Mood { get; set; }
}
这样你就可以写:

var people = from guy in guyArray 
             group guy by guy.Name into g
             select new { 
                 Name = g.Key,
                 Count = g.Count(),
                 HappyCount = g.Count(x => x.Mood == Mood.Happy),
                 SadCount = g.Count(x => x.Mood == Mood.Sad),
                 OkayCount = g.Count(x => x.Mood == Mood.Okay)
             };

至少添加一个到另一个问题的链接,像这样我不清楚我需要什么。谢谢你的帮助。我确实有这个解决方案,但希望将这些结果列在上面的一个列表中。是的,jason one已经足够好了。我只是想把事情弄清楚
var people = from guy in guyArray 
             group guy by guy.Name into g
             select new { 
                 Name = g.Key,
                 Count = g.Count(),
                 HappyCount = g.Count(x => x.Mood == Mood.Happy),
                 SadCount = g.Count(x => x.Mood == Mood.Sad),
                 OkayCount = g.Count(x => x.Mood == Mood.Okay)
             };
To do so: 

   class Guy
    {
        public int age; public string name; string mood;
        public Guy( int age, string name,string mood ) {
            this.age = age;
            this.name = name;
            this.mood = mood;
        }

    }

    class Program
    {
        static void Main( string[] args ) {
           var GuyArray = new Guy[] { 
new Guy(22,"John", "happy"),new Guy(25,"John", "sad"),new Guy(27,"John", "ok"),
new Guy(29,"John", "happy"),new Guy(12,"Jack", "happy"),new Guy(32,"Jack", "happy"),
new Guy(52,"Jack", "happy"),new Guy(100,"Abe", "ok")};


        var peepsSad = from f in GuyArray where f.mood=="sad" group f by f.name into g select new { name = g.Key, count = g.Count() };

 var peepsHappy = from f in GuyArray where f.mood=="happy" group f by f.name into g select new { name = g.Key, count = g.Count() };

 var peepsOk = from f in GuyArray where f.mood=="ok" group f by f.name into g select new { name = g.Key, count = g.Count() };


            foreach ( var record in peepsSad ) {
                Console.WriteLine( record.name + " : " + record.count );
            }

  foreach ( var record in peepsHappy ) {
                Console.WriteLine( record.name + " : " + record.count );
            }

  foreach ( var record in peepsOk ) {
                Console.WriteLine( record.name + " : " + record.count );
            }

        }
    }