1

可悲的是,我以前做过。我记得我想通了。今天,我似乎不记得如何做到这一点。

所以你有这个列表:

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.  
}

上面评论中的问题。将它们连接在一起后如何订购它们?

4

4 回答 4

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();
}
于 2013-09-23T18:29:08.073 回答
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>

于 2013-09-23T18:26:08.467 回答
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);
于 2013-09-23T18:27:25.590 回答
1

只需添加:

return firstTaters.Concat(secondTaters).OrderByDescending(el => el.DateCreated);
于 2013-09-23T18:27:37.757 回答