Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

c# - Combining The Results Of Two Linq Queries Into A Single Var?

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; //???

Thanks!

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...