Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...