将两个Linq查询的结果合并到单个变量中? [英] Combining The Results Of Two Linq Queries Into A Single Var?

查看:219
本文介绍了将两个Linq查询的结果合并到单个变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数据库,其中包含两个设计完全相同的表.如何最有效地合并这两个查询的结果?我知道我可以将每个结果放入字典或数组等中,但是我想有一种更简单的方法可以做到这一点.

I have two different databases that contain two tables with the exact same design. How do I combine the results from these two queries most efficiently? I know I can put the results of each into a dictionary or array or whatever, but I would like to think that there is a simpler way to do this.

var db1 = //A database object connected to database1
var db2 = //A database object connected to database2

var result1 = db1.table.where(a=>a.value>0);
var result2 = db2.table.where(a=>a.value>0);

var resultSum = result1+result2; //???

谢谢!

附录:Concat或Union,对于这种特殊情况哪个更好,为什么?谢谢!

Addendum: Concat or Union, which is better for this particular case and why? Thanks!

推荐答案

您只能合并相同类型的枚举,可以将它们都投影到相同的类中,然后将它们连接起来:

You can only combine enumerations of the same type, you could project both to a common class and then concatenate them:

var result1 = db1.table.Where(a=>a.value>0).Select( x=> new Foo() { /*set props*/ });
var result2 = db2.table.Where(a=>a.value>0).Select( x=> new Foo() { /*set props*/ });

var resultSum = result1.Concat(result2);

这篇关于将两个Linq查询的结果合并到单个变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆