我有一个看起来像这样的表:
FruitID | FruitDate | FruitType
23 | 10/20/14 | 3
32 | 10/12/14 | 3
39 | 09/23/14 | 3
有很多FruitTypes
,我想提取一个包含最后两个Fruit的对象模型列表,例如{FruitID : 23, Date: 10/20}, {FruitID : 32, Date: 10/12}
这就是我所拥有的:
var Output = (from f in MyDC.Fruits
where ... some condition
orderby f.FruitDate descending
group f by f.FruitType in FruitGroups
select.... new FruitModel()
{
FruitID = ...
FruitDate = ...
FruitType = ...
}).ToList();
我遇到最新问题2.如何找回每个FruitType
的最后2个列表?
感谢。
答案 0 :(得分:1)
var Output = (from f in MyDC.Fruits
where ... some condition
orderby f.FruitDate descending
group f by f.FruitType in FruitGroups
select.... new FruitModel()
{
FruitID = ...
FruitDate = ...
FruitType = ...
}).Take(2).ToList();
后续编辑
添加了缺少的from
子句;这是查询:
var Output = (from f in MyDC.Fruits
where ... some condition
orderby f.FruitDate descending
group f by f.FruitType in FruitGroups
from t in FruitGroups
select new FruitModel()
{
FruitID = t.FruitID,
FruitDate = t.FruitDate
FruitType = t.FruitType
}).Take(2).ToList();
答案 1 :(得分:0)
试试这个: -
var query = from fruit in fruitsData
group fruit by fruit.FruitType into g
select new
{
FruitType = g.Key,
Fruits = g.OrderByDescending(x => x.FruitDate).Take(2)
};
工作Fiddle。