我需要对数据进行分组,以便通过条件获得不同的结果
我的实体是:
public class MyTestCollection {
public Int32 Id { get; set; }
public List<MyTestItem> Tests { get; set; }
}
public class MyTestItem {
public long Id { get; set; }
public List<DateItem> Dates { get; set; }
public MyTestItem(long id, List<DateItem> list) {
Id = id;
Dates = list;
}
}
public class DateItem {
public DateTime? DateValue { get; set; }
public DateTime? DateValueOne { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public RateItem Rate { get; set; }
public DateItem(DateTime date, RateItem rate) {
DateValue = date;
Rate = rate;
}
public DateItem(KeyValuePair<DateTime, DateTime> dates , RateItem rate, bool isFormat = true) {
if (isFormat) {
DateValue = dates.Key;
DateValueOne = dates.Value;
} else {
DateFrom = dates.Key;
DateTo = dates.Value;
}
Rate = rate;
}
}
public class RateItem {
public Int32 Value1 { get; set; }
public Int32 Value2 { get; set; }
public Int32 Value3 { get; set; }
public RateItem(int value1, int value2, int value3) {
Value1 = value1;
Value2 = value2;
Value3 = value3;
}
}
我需要以不同的方式从数据表中填充它。现在我将实体DataItem
填入我从表中得到的一个日期 - 每天我有价值的意思,我想知道如果RateItem
相同的话,我会知道如何填写它很多天。在我的示例中,需要实现构造函数public DateItem(KeyValuePair<DateTime, DateTime> dates , RateItem rate, bool isFormat = true)
的登录。只有一个人希望它应该是一个完整的linq,顺序如何从表中获取
现在的逻辑是:
public List<MyTestCollection> GetItems() {
var item = GetData().AsEnumerable().GroupBy(row => new { ID = row.Field<int>("MainId") })
.Select(
testColGroup =>
new MyTestCollection()
{
Id = testColGroup.Key.ID,
Tests = testColGroup.GroupBy(row => new { Id = row.Field<int>("Id") })
.Select(itemGroup => new MyTestItem(itemGroup.Key.Id,
itemGroup.GroupBy(row => row.Field<DateTime>("date"))
.Select(dategroup => new DateItem(dategroup.Key.Date,
new RateItem(dategroup.Select(i => i.Field<int>("value1")).FirstOrDefault()
, dategroup.Select(j => j.Field<int>("value2")).FirstOrDefault()
, dategroup.Select(j => j.Field<int>("value2")).FirstOrDefault())))
.ToList()))
.ToList()
}).ToList();
return item;
我的测试数据表:
static DataTable GetTable() {
var table = new DataTable();
table.Columns.Add("MainId", typeof(Int32));
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("Date",typeof(DateTime));
table.Columns.Add("value1", typeof(Int32));
table.Columns.Add("value2", typeof(Int32));
table.Columns.Add("value3", typeof(Int32));
// Row 1
var row = table.NewRow();
row["MainId"] = 1;
row["Id"] = 1;
row["Date"] = "06/10/2015";
row["value1"] = 558;
row["value2"] = 206;
row["value3"] = 352;
table.Rows.Add(row);
// Row 2
row = table.NewRow();
row["MainId"] = 1;
row["Id"] = 1;
row["Date"] = "06/11/2015";
row["value1"] = 558;
row["value2"] = 206;
row["value3"] = 352;
table.Rows.Add(row);
// Row 3
row = table.NewRow();
row["MainId"] = 1;
row["Id"] = 1;
row["Date"] = "06/12/2015";
row["value1"] = 558;
row["value2"] = 206;
row["value3"] = 352;
table.Rows.Add(row);
return table;
}
我希望找到解决方案也有帮助
UPD:
这是可能的表
MainId Id Date value1 value2
1 1 6/10/2015 558 206
1 1 6/11/2015 558 206
1 1 6/12/2015 111 111
1 2 6/10/2015 111 206
1 2 6/11/2015 111 206
1 2 6/12/2015 558 206
1 2 6/13/2015 558 206
所有这一切:
<request>
<main>1</main>
<sku id="1">
**<date datefrom="6/10/2015" dateto="6/11/2015">**
<value1>558</value1>
<value2>206</value2>
</date>
**<date value="6/12/2015">**
<value1>111</value1>
<value2>111</value2>
</date>
</sku>
<sku id="2">
<date datefrom="6/10/2015" dateto="6/11/2015">
<value1>111</value1>
<value2>206</value2>
</date>
<date datefrom="6/12/2015" dateto="6/13/2015">
<value1>558</value1>
<value2>206</value2>
</date>
</sku>
</request>