猿问

如何从 List<T> 值创建新列?

我有一个这样的列表:


internal class Order

{

    public Order()

    {

    }


    public string ProductID { get; set; }

    public int Amount { get; set; }

    public string Date { get; set; }

}


List<Order> orderList = new List<Order>() 

{

    new Order(){ ProductID="12345", Amount=300, Date = "2018-12-19"},

    new Order(){ ProductID="12345", Amount=0, Date = "2018-12-20"},

    new Order(){ ProductID="12345", Amount=200, Date = "2018-12-21"},

    new Order(){ ProductID="12345", Amount=250, Date = "2018-12-22"},

    new Order(){ ProductID="12345", Amount=30, Date = "2018-12-23"},

    new Order(){ ProductID="67898", Amount=20, Date = "2018-12-20"},

    new Order(){ ProductID="67898", Amount=30, Date = "2018-12-21"},

    new Order(){ ProductID="67898", Amount=40, Date = "2018-12-22"},

    new Order(){ ProductID="67898", Amount=50, Date = "2018-12-23"},

    new Order(){ ProductID="67898", Amount=130, Date = "2018-12-24"}

};

在这种情况下,当我将列表转换DataTable为 Excel 并将其导出时,我得到以下信息:

我想从Date值创建新列并按ProductID列出它们。如何将此列表转换DataTable为如下所示:


http://img3.mukewang.com/62aee1f900016cbf04890061.jpg

ITMISS
浏览 177回答 2
2回答

墨色风雨

GroupBy您可以通过使用and来实现这一点ExpandoObject:var result = new List<ExpandoObject>();foreach (var orders in orderList.GroupBy(obj => obj.ProductID)){&nbsp; &nbsp; var record = new ExpandoObject();&nbsp; &nbsp; foreach (var order in orders.AsEnumerable())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ((IDictionary<string, object>)record).Add(order.Date, order.Amount);&nbsp; &nbsp; }&nbsp; &nbsp; result.Add(record);}

Cats萌萌

你想要一个像 excel 一样的数据透视表。下面的代码创建了两个数据表。您列出的第一个基础。然后它需要第一个表并创建第二个数据透视表using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace ConsoleApplication93{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Order order = new Order();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = order.MakeTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //now create piviot table&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] dates = dt.AsEnumerable().Select(x => x.Field<string>("Date")).Distinct().ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable pivot = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pivot.Columns.Add("ProductID", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string date in dates)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pivot.Columns.Add(date, typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("ProductID")).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(var group in groups)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow newRow = pivot.Rows.Add();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newRow["ProductID"] = group.Key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var col in group)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newRow[col.Field<string>("Date")] = col.Field<int>("Amount");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; internal class Order&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Order()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public string ProductID { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int Amount { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string Date { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DataTable MakeTable()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Order> orderList = new List<Order>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="12345", Amount=300, Date = "2018-12-19"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="12345", Amount=0, Date = "2018-12-20"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="12345", Amount=200, Date = "2018-12-21"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="12345", Amount=250, Date = "2018-12-22"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="12345", Amount=30, Date = "2018-12-23"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="67898", Amount=20, Date = "2018-12-20"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="67898", Amount=30, Date = "2018-12-21"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="67898", Amount=40, Date = "2018-12-22"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="67898", Amount=50, Date = "2018-12-23"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Order(){ ProductID="67898", Amount=130, Date = "2018-12-24"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var prop in this.GetType().GetProperties())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string typeName = prop.PropertyType.FullName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type systemType = System.Type.GetType(typeName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(prop.Name, systemType);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Order row in orderList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object[] data = row.GetType().GetProperties().Select(x => x.GetValue(row, null)).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dt;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答