tags:

views:

27

answers:

2

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?

A: 

Even

var result = Enumerable.Concat(list1, list2);

doesn't work?

And be sure that lists are empty, not null:

var result = Enumerable.Concat(
    list1 ?? Enumerable.Empty<MediaType>()
    list2 ?? Enumerable.Empty<MediaType>());

Also try:

var result = Enumerable.Concat(
    list1.AsEnumerable(),
    list2.AsEnumerable());
abatishchev
No i have try both:(
Fraz Sundal
@Fraz: Please try each of my variant and report the results
abatishchev
+2  A: 

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