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 want to merge the records of two IQueryable lists in C#. I try

IQueryable<MediaType> list1 = values;
IQueryable<MediaType> list2 = values1;
obj.Concat(obj1);

and

IQueryable<MediaType> list1 = values;
IQueryable<MediaType> list2 = values1;
obj.Union(obj1);

but if list1 is empty then the resultant list is also empty. In my case either list1 can be empty but list2 can have records. How should i merge them?

See Question&Answers more detail:os

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

1 Answer

You're not using the return value - just like all other LINQ operators, the method doesn't change the existing sequence - it returns a new sequence. So try this:

var list3 = list1.Concat(list2);

or

var list4 = list1.Union(list2);

Union is a set operation - it returns distinct values.

Concat simply returns the items from the first sequence followed by the items from the second sequence; the resulting sequence can include duplicate items.

You can think of Union as Concat followed by Distinct.


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