可悲的是,我以前做过这件事。我记得搞清楚了。今天,我似乎无法回想起如何做到这一点。
所以你有这个清单:
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.
}
以上评论中的问题。如何将它们加在一起后订购?
答案 0 :(得分:2)
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>
答案 1 :(得分:2)
使用Concat
,或者如果您想要不同的结果,请使用Union
var concated =
firstTaters.Concat(secondTaters).OrderByDescending(html => html.date_created);
//Gives distinct values
var unioned =
firstTaters.Union(secondTaters).OrderByDescending(html => html.date_created);
答案 2 :(得分:2)
或者你可以像下面的例子那样做:
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;
return (
from first in firstTaters
join second in secondTaters on secondTaters.someField equals second.someField
select new
{
....
....
}
).toList();
}
答案 3 :(得分:1)
只需添加:
return firstTaters.Concat(secondTaters).OrderByDescending(el => el.DateCreated);