C#组嵌套列表

时间:2018-12-11 16:59:03

标签: c#

我有一个值列表,如:

OrderId_1, Number_1, Date, ItemId, ItemName, ItemDesc, Address1Id Address1Zip, Address1City, Address2Id Address2Zip, Address2City
.
.
.
OrderId_n, Number_n, Date, ItemId, ItemName, ItemDesc, Address1Id Address1Zip, Address1City, Address2Id Address2Zip, Address2City

我想将它们分组为嵌套对象,例如:

List<Order>
   Order: OrderId, Number, Date, ItemList, Address1, Address2
   ItemList: ItemId, ItemName, ItemDesc
   Address1: Address1Id Address1Zip, Address1City
   Address2: Address2Id Address2Zip, Address2City

我尝试过:

List<Order> list = orderFromDB.ToList()
                .GroupBy(a => a.OrderId)
                .Select(
                    g =>
                        new Order
                        {
                            OrderId = g.Key,
                            Number= g.Number, <<< No Number Property here

样本: 我有清单:

OrderId1, Number1, Date1, ItemId1, ItemName1, ItemDesc1, Address1Id1 Address1Zip1, Address1City1, Address2Id1 Address2Zip1, Address2City1
OrderId1, Number1, Date1, ItemId2, ItemName2, ItemDesc2, Address1Id1 Address1Zip1, Address1City1, Address2Id1 Address2Zip1, Address2City1
OrderId1, Number1, Date1, ItemId3, ItemName3, ItemDesc3, Address1Id2 Address1Zip2, Address1City2, Address2Id2 Address2Zip2, Address2City2

To format it like:
OrderId1, Number1, Date1, 
                         Items as List<Items>: 
                                ItemId1, ItemName1, ItemDesc1
                                ItemId2, ItemName2, ItemDesc2
                                ...
                         Address1: Address1Id1, Address1Zip1, Address1City1
                         Address2: Address2Id2, Address2Zip2, Address2City2
.
.
. 
the other orders.

我如何将它们分组为订单列表?

谢谢!

2 个答案:

答案 0 :(得分:1)

假设(除其他事项外)您无法修复基础数据源,也许:

class OrderItem
{
    public int OrderId;
    public int Number;
    public DateTime Date;
    public int ItemId;
    public string ItemName;
    public string ItemDesc;
    public string Address1Id;
    public string Address1Zip;
    public string Address1City;
    public string Address2Id;
    public string Address2Zip1;
    public string Address2City;
}
class Item
{
    public int ItemID;
    public string ItemName;
    public string ItemDesc;
}
class Address
{
    public string AddressId;
    public string AddressZip;
    public string AddressCity;
}
class Order
{
    public int OrderId;
    public int Number;
    public DateTime Date;
    public Address Address1;
    public Address Address2;
    public List<Item> Items;
}
static void Main(string[] args)
{
    List<OrderItem> orderItems = new List<OrderItem>
    {
        new OrderItem()
        {
                OrderId = 1, Number=1, Date = DateTime.Parse("2018-12-11"),
                ItemId = 123, ItemName = "Name 123", ItemDesc = "Item 123",
                Address1Id = "1", Address1City = "New York", Address1Zip = "10001",
                Address2Id = "2", Address2City = "Boston", Address2Zip1 = "02101"
        }
    };

    var query = orderItems.GroupBy(o => new
    {
        o.OrderId,
        o.Number,
        o.Date,
        o.Address1City,
        o.Address1Id,
        o.Address1Zip,
        o.Address2City,
        o.Address2Id,
        o.Address2Zip1
    },
    (group, orders) => new Order
    {
        OrderId = group.OrderId,
        Number = group.Number,
        Date = group.Date,
        Address1 = new Address() { AddressCity = group.Address1City, AddressId = group.Address1Id, AddressZip = group.Address1Zip },
        Address2 = new Address() { AddressCity = group.Address2City, AddressId = group.Address2Id, AddressZip = group.Address2Zip1 },
        Items = (from o in orders select new Item() { ItemID = o.ItemId, ItemDesc = o.ItemDesc, ItemName = o.ItemName  }).ToList()
    });

    //do something with query
    return 0;
}

代码已编译,但尚未经过实际测试。

答案 1 :(得分:0)

部分问题是您的体系结构不好。

如果我正在阅读此书,则说明:

  • 项目-由要运输的ID,名称,描述和地址组成 到。
  • 订单-由一个ID,一个数字,一个日期和一个集合组成 项目。

太好了!所以您的课程应该看起来像这样:

public class Order
{
    public int orderID;
    public string orderNumber;
    public DateTime orderDate;
    public List<Item> itemsInOrder;
}
public class Item
{
    public int itemID;
    public string name;
    public string description;
    public Address address;
}
public class Address
{
    public int addressID;
    public string zip;
    //etc
}

您的数据库后端也应该具有这种规范化的结构。

好吧,如果您的数据库后端没有被规范化,而所有这些数据都只是转储到行中了?

然后,您仍然至少要在C#层中具有适当的类结构。不要试图将事物仅仅表示为嵌套的值列表。实际构造您的数据。

也许像这样:

public class Order
{
    // same data properties as earlier code sample
    public static List<Order> GenerateFromRawDataLines(List<List<string>> rawData)
    {
        // do a linq GroupBy on rawData's orderID column
        // for each item in the GroupBy:
            // generate a new Order instance, populate its data members
            // for each item in the subgrouping:
                // add a new Item instance with an embedded Address instance.
    }
}
public class Item
{
    // same data properties as earlier code sample
    public Item(List<string> rawDataLine)
    {
        this.itemID = rawDataLine[3];
        this.name = rawDataLine[4];
        // etc - translating a raw List<string> of data values into a class instance
    }
}