简体   繁体   English

创建列表linq的n个列表

[英]create n lists of list linq

在此处输入图片说明 I have a simple list 我有一个简单的清单

var list =
            AppUtils.db.GetDataTable("dbo.RankSelectChart", view) // stopred procedure and getting datatable 
                       .AsEnumerable()
                       .Select(i => new 
                                   { 
                                     Date = i.Field<DateTime>("lastDatetime"),
                                     P1 = i.Field<decimal>("p1"),
                                     P2 = i.Field<decimal>("P2"), 
                                     P3..... P(n) 
                                   }
                               )
                       .ToList()
                       .OrderBy(x => x.Date);

than i want to get a list of lists or dictionary like List<Dictionary<Datetime, Decimal>> means Dictionary<Date, P1> .... Dictionary<Date, P(n)> 比我想获得列表或字典的列表,例如List<Dictionary<Datetime, Decimal>>意味着Dictionary<Date, P1> .... Dictionary<Date, P(n)>

how to write algorithm which is not depend how many P we have 如何编写算法,这不取决于我们有多少P

As it stands, you will need to use reflection to access the properties: 就目前而言,您将需要使用反射来访问属性:

var result = new[]{"P1", "P2", "P3", ...}.Select(p => list.ToDictionary(
    i => i.Date, 
    i => i.GetType().GetProperty(p).GetValue(i)));

However, if you could avoid creating your list in the first place and just pull from the data table directly, it may be easier. 但是,如果您可以避免首先创建列表,而直接从数据表中提取,则可能会更容易。

var dt = AppUtils.db.GetDataTable("dbo.RankSelectChart", view); // stopred procedure and getting datatable 
var pColumns = dt.Columns.Cast<DataColumn>()
    .Where(c => c.ColumnName.StartsWith("p"));
var result = pColumns
        .Select(p => dt.AsEnumerable().ToDictionary(
                i => i.Field<DateTime>("lastDatetime"),
                i => i.Field<DateTime>(p.ColumnName)))
        .ToList();

If 'Date' is unique for all records in 'list', then you can use reflection to get the P(i) value for a record in list. 如果“日期”对于“列表”中的所有记录都是唯一的,则可以使用反射来获取列表中记录的P(i)值。 Like so: 像这样:

  // build sample data
  var list = Enumerable.Range(0, 10)
    .ToList()
    .Select(x => new {
      Date = DateTime.Now.AddMinutes(x),
      P1 = new Decimal(x),
      P2 = new Decimal(x + 1),
      P3 = new Decimal(x + 2)
    })
    .ToList();

  // list partionned by date; assumes that Date is unique in list
  List<Dictionary<DateTime, Decimal>> partitionedList;

  if (list.Count == 0) {

    partitionedList = new List<Dictionary<DateTime, Decimal>>();

  } else {

    var n = 3;
    var listElementType = list[0].GetType();

    partitionedList = Enumerable.Range(1, n)
      .Select(x => {
        var prop = listElementType.GetProperty("P" + x);
        var pList = list.ToDictionary(
          ll => ll.Date,
          ll => (Decimal)prop.GetValue(ll));
        return pList;
      })
      .ToList();
  }

If 'Date' is not unique, then it cannot be the key to a dictionary and the desired data structure is not achievable. 如果“日期”不是唯一的,则它不能成为字典的键,并且无法实现所需的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM