Home > front end >  Merge two ore more list in a list?
Merge two ore more list in a list?

Time:12-20

I have two queries. I want to assign the list values ​​returned from these two queries to a single list, send it to the view and meet it in the view. My goal is to learn to work with lists.

`

var List1= c.ETs.Where(p => p.prop != "yes").ToList();
var List2 = c.ETs.Where(p => p.prop == "yes").ToList();

`

CodePudding user response:

This will combine the values of both lists in a single list (the final output will be stored in List1):

List1.AddRange(List2);

CodePudding user response:

Hi Ahmet you can try something like this but note you will need Linq:

var list3 = List1.Concat(List2).ToList();

or if you have more than 2 lists:

var list3 = list1.Concat(list2)
                              .Concat(list3)
                              .ToList();

Another method:

var list3 = List1.AddRange(List2);

Both above will create a new list containing both lists' items.

CodePudding user response:

You can merge two lists into one with this:

var newList = List1
    .Concat(List2)
    .ToList();

Though you could drop the ToList and work directly with the IEnumerable which means you don't need to create a new object.

However, this doesn't even need to be two queries since the Where clauses of both are opposite so they include the entire table, you could do:

var list = c.ETs.ToList();

Or if you want to have two different clauses that aren't simply opposites:

var list = ct.ETs
    .Where(p => p.prop == "yes" || p.prop == "no")
    .ToList()` for example
  • Related