我有一些这样的结构列表:
struct va_data
{
public int item_id;
public int type_id;
public double item_value;
public DateTime value_date;
}
我尝试按 type_id 对列表进行分组,然后取 value_date 最大的项目,然后按 item_id 分组,只取 item_value 最小的项目
有我的语法
from x in dataList
group x by x.type_id into grouped
select grouped.Where(x => x.value_date == grouped.Max(y => y.value_date))
.GroupBy(x => x.item_id) // and here i was stuck.
Example
var dataList = new []
{
new va_data {item_id = 1, type_id = 1, item_value = 0, value_date = "2013.07.29"},
new va_data {item_id = 1, type_id = 1, item_value = 1, value_date = "2013.07.30"},
new va_data {item_id = 2, type_id = 1, item_value = 0, value_date = "2013.07.29"},
new va_data {item_id = 2, type_id = 1, item_value = 1, value_date = "2013.07.29"},
new va_data {item_id = 4, type_id = 2, item_value = 5, value_date = "2013.07.29"},
new va_data {item_id = 4, type_id = 3, item_value = 9, value_date = "2013.07.30"},
};
The result must be
var dataListResult = new []
{
new va_data {item_id = 1, type_id = 1, item_value = 1, value_date = "2013.07.30"},
new va_data {item_id = 2, type_id = 1, item_value = 0, value_date = "2013.07.29"},
new va_data {item_id = 4, type_id = 2, item_value = 5, value_date = "2013.07.29"},
}