使用 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
有人可以告诉我我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您尝试在
Select
调用中使用x
,但它超出了范围。另外,您尝试直接使用Name
,但它不是分组键的一部分。最后,您尝试在“选择”中选择单个订单,而不是一组订单。我认为你想要这个:假设 CustID 是唯一的(这样你每次都会得到相同的名称),你可以仅按 CustID 进行分组,然后根据 Achim 的回答将第一个客户作为名称 - 但我个人更喜欢分组键中的所有“每个客户”数据,因为这就是您要分组的逻辑。
编辑:要回答编辑后的问题,您可以填充
OrderList
属性,如下所示:...但说实话,到那时它会变得非常混乱。
Yes, you're trying to use
x
within theSelect
call, but it's out of scope. Also, you're trying to use theName
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: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:... but to be honest it's getting pretty messy by then.