C#稍微复杂的linq

时间:2011-09-22 17:20:57

标签: 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 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的建议使用上面的约翰,杰克和安倍的出现次数。但是,如果问题稍微复杂一点

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

上面的代码非常适合检索不同名称的出现次数,但如果我需要名称的出现次数以及每个人的快乐,悲伤或好的出现次数,该怎么办呢。即输出是:名称,名称计数,满意的名字计数,悲伤的名称数,可以的名称数。如果linq不是最佳解决方案,我准备听取所有替代方案。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:7)

坦率地说,目前尚不清楚你是否想要幸福的总人数,或者想要通过名字感到满意的总人数(也是为了伤心,好吧)。我会给你一个解决方案,可以给你们两个。

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

然后:

foreach(nameGroup in nameGroups) {
    Console.WriteLine("Name = {0}, Count = {1}, Happy count = {2}, Sad count = {3}, Okay count = {4}", nameGroup.name, nameGroup.count, nameGroup.happy, nameGroup.sad, nameGroup.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)
             };

答案 1 :(得分:1)

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

        }
    }