使用 linq 进行分组

发布于 2024-11-02 17:41:12 字数 2249 浏览 1 评论 0原文

class Customer
{
    public string ID { get; set; }
    public string Name { get; set; }
    public List<Order> OrderList { get; set; }
}

class Order
{
    public string OrderNumber { get; set; }
    public List<Item> ItemList { get; set; }
}

class Item
{
    public string ItemName { get; set; }
}


    static void Main(string[] args)
    {

        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.Add("CustID");
        dt.Columns.Add("Name");
        dt.Columns.Add("OrderNumber");
        dt.Columns.Add("ItemName");

        dt.Rows.Add("1", "A", "O1", "Item1");
        dt.Rows.Add("1", "A", "O1", "Item2");
        dt.Rows.Add("1", "A", "O1", "Item3");

        dt.Rows.Add("1", "A", "O2", "Item4");
        dt.Rows.Add("1", "A", "O2", "Item5");

        dt.Rows.Add("1", "A", "O3", "Item1");

        dt.Rows.Add("2", "B", "O4", "Item1");
        dt.Rows.Add("2", "B", "O4", "Item2");

        dt.Rows.Add("2", "B", "O5", "Item3");


        dt.AcceptChanges();

        List<DataRow> dList = dt.AsEnumerable().ToList();


        //This is not working
        var qry = dList.GroupBy(x => x["CustID"])
                        .Select(g => new Customer
                        {
                            ID = g.Key.ToString(),
                            Name = g.First()["Name"].ToString(),
                            OrderList = g.GroupBy(t=> t["OrderNumber"]).Select(x => new Order
                            {

                                OrderNumber = x.First()["OrderNumber"].ToString(),
                                ItemList = g.GroupBy(u => u["OrderNumber"]).Select(y => new Item { ItemName = y.First()["ItemName"].ToString() }).ToList()
                            }).ToList()
                        }).ToList();
    }

我正在尝试获得这样的输出

Custtomer.Name="A"
Customer.ID="1"
Customer.OrderList = ListOfOrders with "01", "02", "03"
Customer.OrderList[0].ItemList = ListOfItems with that order Item1, Item2, Item3


Custtomer.Name="B"
Customer.ID="2"
Customer.OrderList = ListOfOrders with "04", "05"
Customer.OrderList[0].ItemList = ListOfItems with that order Item4, Item5

有人可以告诉我我的 qry 有什么问题吗?

class Customer
{
    public string ID { get; set; }
    public string Name { get; set; }
    public List<Order> OrderList { get; set; }
}

class Order
{
    public string OrderNumber { get; set; }
    public List<Item> ItemList { get; set; }
}

class Item
{
    public string ItemName { get; set; }
}


    static void Main(string[] args)
    {

        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.Add("CustID");
        dt.Columns.Add("Name");
        dt.Columns.Add("OrderNumber");
        dt.Columns.Add("ItemName");

        dt.Rows.Add("1", "A", "O1", "Item1");
        dt.Rows.Add("1", "A", "O1", "Item2");
        dt.Rows.Add("1", "A", "O1", "Item3");

        dt.Rows.Add("1", "A", "O2", "Item4");
        dt.Rows.Add("1", "A", "O2", "Item5");

        dt.Rows.Add("1", "A", "O3", "Item1");

        dt.Rows.Add("2", "B", "O4", "Item1");
        dt.Rows.Add("2", "B", "O4", "Item2");

        dt.Rows.Add("2", "B", "O5", "Item3");


        dt.AcceptChanges();

        List<DataRow> dList = dt.AsEnumerable().ToList();


        //This is not working
        var qry = dList.GroupBy(x => x["CustID"])
                        .Select(g => new Customer
                        {
                            ID = g.Key.ToString(),
                            Name = g.First()["Name"].ToString(),
                            OrderList = g.GroupBy(t=> t["OrderNumber"]).Select(x => new Order
                            {

                                OrderNumber = x.First()["OrderNumber"].ToString(),
                                ItemList = g.GroupBy(u => u["OrderNumber"]).Select(y => new Item { ItemName = y.First()["ItemName"].ToString() }).ToList()
                            }).ToList()
                        }).ToList();
    }

I am trying to get output like this

Custtomer.Name="A"
Customer.ID="1"
Customer.OrderList = ListOfOrders with "01", "02", "03"
Customer.OrderList[0].ItemList = ListOfItems with that order Item1, Item2, Item3


Custtomer.Name="B"
Customer.ID="2"
Customer.OrderList = ListOfOrders with "04", "05"
Customer.OrderList[0].ItemList = ListOfItems with that order Item4, Item5

Can someone tell me whats wrong with my qry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-11-09 17:41:12

是的,您尝试在 Select 调用中使用 x,但它超出了范围。另外,您尝试直接使用 Name,但它不是分组键的一部分。最后,您尝试在“选择”中选择单个订单,而不是一组订单。我认为你想要这个:

var qry = dList.GroupBy(x => new { CustID = (string) x["CustID"],
                                   Name = (string) x["Name"] })
               .Select(g => new Customer {
                          ID = g.Key.CustID,
                          Name = g.Key.Name,
                          OrderList = g.Select(x => new Order {
                                 // Indentation bad to avoid scrolling!
                                 OrderNumber = (string) x["OrderNumber"] })
                                       .ToList()
                       })
               .ToList();

假设 CustID 是唯一的(这样你每次都会得到相同的名称),你可以仅按 CustID 进行分组,然后根据 Achim 的回答将第一个客户作为名称 - 但我个人更喜欢分组键中的所有“每个客户”数据,因为这就是您要分组的逻辑

编辑:要回答编辑后的问题,您可以填充OrderList属性,如下所示:

OrderList = g.GroupBy(t=> (string) t["OrderNumber"]).Select(x => new Order
{
    OrderNumber = x.Key,
    ItemList = x.Select(y => new Item { 
                              ItemName = (string) y["ItemName"]
                        }).ToList()
}).ToList()

...但说实话,到那时它会变得非常混乱。

Yes, you're trying to use x within the Select call, but it's out of scope. Also, you're trying to use the Name directly, but it's not part of the grouping key. Finally, you're trying to select a single order in your Select, not a group of them. I think you want this:

var qry = dList.GroupBy(x => new { CustID = (string) x["CustID"],
                                   Name = (string) x["Name"] })
               .Select(g => new Customer {
                          ID = g.Key.CustID,
                          Name = g.Key.Name,
                          OrderList = g.Select(x => new Order {
                                 // Indentation bad to avoid scrolling!
                                 OrderNumber = (string) x["OrderNumber"] })
                                       .ToList()
                       })
               .ToList();

Assuming that CustID is unique (so you'll get the same Name each time) you can group just by CustID and then take the first customer for the Name, as per Achim's answer - but personally I prefer to group all the "per customer" data in the key, as that's logically what you're grouping on.

EDIT: To answer the edited question, you could populate the OrderList property something like this:

OrderList = g.GroupBy(t=> (string) t["OrderNumber"]).Select(x => new Order
{
    OrderNumber = x.Key,
    ItemList = x.Select(y => new Item { 
                              ItemName = (string) y["ItemName"]
                        }).ToList()
}).ToList()

... but to be honest it's getting pretty messy by then.

遇到 2024-11-09 17:41:12
var qry = dList.GroupBy(x => x["CustID"]).Select(g => new Customer
        {
            ID = g.Key.ToString(),
            Name = g.First()["Name"],
            OrderList=g.Select(o => new List<Order>(new Order{ OrderNumber=o["OrderNumber"]}))
        }).ToList();
var qry = dList.GroupBy(x => x["CustID"]).Select(g => new Customer
        {
            ID = g.Key.ToString(),
            Name = g.First()["Name"],
            OrderList=g.Select(o => new List<Order>(new Order{ OrderNumber=o["OrderNumber"]}))
        }).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文