使用linq分组


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
谁能告诉我我的问题出了什么问题?

是的,您试图在Select调用中使用x,但它超出了范围。此外,您正试图直接使用Name,但它不是分组密钥的一部分。最后,您尝试在select中选择单个订单,而不是一组订单。我想你需要这个:

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()

…但老实说,到那时已经很乱了。

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

相关内容

  • 没有找到相关文章

最新更新