C# C/LINQ到SQL-来自两个不同结果集的顺序组合结果
可悲的是,我以前也这么做过。我记得我弄明白了。今天,我似乎不记得该怎么做了 因此,您有以下列表:C# C/LINQ到SQL-来自两个不同结果集的顺序组合结果,c#,linq,linq-to-sql,concatenation,C#,Linq,Linq To Sql,Concatenation,可悲的是,我以前也这么做过。我记得我弄明白了。今天,我似乎不记得该怎么做了 因此,您有以下列表: public List<taters> getTaters(){ var firstTaters = from s in n.veggies where s.active == true select s.html; var secondTaters = from s in n.roo
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或更改签名以返回iQueryable或IEnumerable
在过滤之前,还要尝试在两个集合上使用串联,以避免代码重复可能会更慢,但更易于维护
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或更改签名以返回iQueryable或IEnumerable如果想要不同的结果,请使用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);
如果希望得到不同的结果,请使用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);
只需添加以下内容:
return firstTaters.Concat(secondTaters).OrderByDescending(el => el.DateCreated);
只需添加以下内容:
return firstTaters.Concat(secondTaters).OrderByDescending(el => el.DateCreated);
或者您也可以像下面的示例中那样执行此操作:
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();
}
或者您也可以像下面的示例中那样执行此操作:
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();
}
FirstStaters.ConcatSecondStaters.OrderByd=>d.date\u created…?是的,我可以将这两个结果连接到一个结果列表中,就像FirstStaters.ConcatSecondStaters这样,但是…我想按“date\u created”排序,它们都作为字段。FirstStaters.ConcatSecondStaters.OrderByd=>d.date\u created…?是的,我可以将这两个结果连接到一个结果列表中,如FirstStaters.ConcatSecondStaters,但是……我想按“创建日期”对它们进行排序,它们都作为字段。哇-耶!南达声明!!!!不知怎么忘了。谢谢,谢谢,谢谢!!哇-耶!南达声明!!!!不知怎么忘了。谢谢,谢谢,谢谢!!关于工会的观点很好。如果需要区分的话,肯定很好。谢谢Lambda表达式非常适合这个账单-谢谢!关于工会的观点很好。如果需要区分的话,肯定很好。谢谢Lambda表达式非常适合这个账单-谢谢!我想知道这个表达式或Lambda表达式是否有助于更快/更高效的选择……我忘了在to list语句之前放order by子句。我喜欢这种方式,因为我可以创建不同的对象提示。当然,您也可以用这种方式创建taters对象。我想知道这个表达式或Lambda表达式是否有助于更快/更高效的选择……我忘了将order by子句放在to list语句之前。我喜欢这种方式,因为我可以创建不同的对象提示。当然,您也可以用这种方式创建taters对象。