需要帮助进行linq查询 [英] Need help making a linq query

查看:61
本文介绍了需要帮助进行linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我不知道我想要什么是可能的linq,但我想创建一个linq语句,通过一个列表输入多个字段并对其进行排序。每个条目都有用户名和类型作为字段我希望返回每个用户名的列表,其中包含每个类型的列表,里面有一个计数。



I到目前为止

Hi,

I don't know if what i want is possible with linq but i am trying to create a linq statement that goes through a list of a type with multiple fields and sort them. Each entry has "Username" and "Type" as fields i am looking to return a list of each Username containing a List of each Type with a count inside.

I have this so far

var users = from p in results group p by p.UserName into g select new { Profile = g.Key, ProfileCount = g.Count() };





这可能吗?怎么样?



谢谢

Carl



Is this possible? and how?

Thank you
Carl

推荐答案

我不是确定我正在读你的问题,但是对我而言,你似乎想要将用户名上的源数据分组,然后将源数据中的类别分组并将数量添加到组中。



如果这是正确的,那么这样的事可能适合你;



I am not sure I'm reading your question right, but I to me it looks like you want to group the source data on Username, and then group the Types in the source data and add count to the group.

If this is correct, then something like this might work for you;

using System;
using System.Linq;

namespace LinqStuff {
    class BusinessObject {
        public string Username { get; set; }
        public string Type { get; set; }
    }

    class Program {
        static void Main(string[] args) {

            var data = new[] {
                new BusinessObject { Username = "Alpha", Type="A" },
                new BusinessObject { Username = "Alpha", Type="A" },
                new BusinessObject { Username = "Bravo", Type="A" },
                new BusinessObject { Username = "Charlie", Type="A" },
                new BusinessObject { Username = "Delta", Type="A" },
                new BusinessObject { Username = "Alpha", Type="B" },
                new BusinessObject { Username = "Alpha", Type="C" },
                new BusinessObject { Username = "Bravo", Type="C" },
                new BusinessObject { Username = "Charlie", Type="C" },
                new BusinessObject { Username = "Delta", Type="C" },
                new BusinessObject { Username = "Bravo", Type="D" }
            };
            var groups = from entry in data
                         group entry by entry.Username
                             into g
                             select new {
                                     Username = g.Key,
                                     Types = from type in g group type by type.Type into t select new { Type = t.Key, Count = t.Count() }
                                 };

            foreach(var e in groups) {
                Console.WriteLine("Username {0};", e.Username);
                foreach(var t in e.Types) {
                    Console.WriteLine("\tType {0}, Count={1}", t.Type, t.Count);
                }
            }
        }
    }
}





如果没有,请详细说明您的源数据是什么样的,以及您希望目标分组看起来像什么,我会调整答案。



希望这有帮助,

Fredrik



If not, please elaborate on what your source data look like, and what you want the target grouping to look like and I'll adjust the answer.

Hope this helps,
Fredrik


这篇关于需要帮助进行linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆