可悲的是,我以前做过这个。我记得弄清楚了。今天,我似乎想不起如何做到这一点。
所以你有这个列表:
public List<taters> getTaters(){
var firstTaters = from s in n.veggies
where s.active == true
select s.html;
var secondTaters = from s in n.roots
where s.active == true
select s.html;
//now here I want to do something to combine the two
//(e.g. a Concat or some such) and
//THEN I want to order the concatenated list of results
//by 'date_created' descending.
}
上述评论中的问题。将它们连接在一起后如何订购?
最佳答案
firstTaters.Concat(secondTaters)
.OrderByDescending(html => html.date_created)
也尝试在过滤之前对两个集合使用连接,以避免代码重复(可能更慢,但更易于维护)
public IEnumerable<taters> getTaters()
{
return from s in n.veggies.Concat(n.roots)
where s.active == true
orderby s.html.date_created descending
select s.html;
}
别忘了调用ToList
或更改签名以返回 IQueryble<taters>
或 IEnumerable<taters>
关于C#/LINQ to SQL - 对来自两个不同结果集的组合结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18966166/