简体   繁体   中英

How do I combine the contents of two lists?

I have two List<int> instances. Now I want to combine them into a third list.

public List<int> oldItemarry1 // storing old item
{
    get 
    { 
        return (List<int>)ViewState["oldItemarry1 "]; 
    }
    set 
    { 
        ViewState["oldItemarry1 "] = value; 
    }
}

public List<int> newItemarry1 // storing new item
{
    get
    { 
        return (List<int>)ViewState["newItemarry1 "]; 
    }
    set 
    { 
        ViewState["newItemarry1 "] = value; 
    }
}

public List<int> Itemarry1 // want to combine both the item
{
    get
    { 
        return (List<int>)ViewState["Itemarry1 "]; 
    }
    set 
    { 
        ViewState["Itemarry1 "] = value; 
    }
}

Please some one tell me how to do that?

LINQ has the Concat method:

return oldItemarry1.Concat(newItemarry1).ToList();

That just puts the list together. LINQ also has Intersect method, which will give you only items that exist in both lists and the Except method, which only gives you items that are present in either, but not both. The Union method give you all items between the two lists, but no duplicates like the Concat method.

If LINQ is not an option, you can just create a new list, add the items from each list to both via AddRange , and return that.

EDIT:

Since LINQ is not an option, you can do it a few ways:

Combine lists with all items, including duplicates:

var newList = new List<int>();
newList.AddRange(first);
newList.AddRange(second);
return newList

Combine without duplicate items

var existingItems = new HashSet<int>();
var newList = new List<int>();

existingItems.UnionWith(firstList);
existingItems.UnionWith(secondList);
newList.AddRange(existingItems);

return newList;

This of course assumes that you're using .NET 4.0, since that is when HashSet<T> was introduced. It's a shame you aren't using Linq, it really excels at things like this.

Use the Union method; it will exclude duplicates.

int[] combinedWithoutDups = oldItemarry1.Union(newItemarry1).ToArray();

You can combine two lists:

List<int> result = new List<int>();
result.AddRange(oldList1);
result.AddRange(oldList2);

The list result now has all the elements of both lists.

Here's one way to approach it:

public List<int> Itemarry1()
{
    List<int> combinedItems = new List<int>();

    combinedItems.AddRange(oldItemarray1);
    combinedItems.AddRange(newItemarray1);

    return combinedItems;
}

As a best practice, try to use IEnumerable rather than List when you can. Then, to make this work best you will want a read-only property:

public IEnumerable<int> Itemarry1 // want to combine both the item
{
    get
    { 
        return ((List<int>)ViewState["oldItemarry1 "]).Concat((List<int>)ViewState["Itemarry1"]); 
    }
}

If you need a point in time combination of two lists into a third list, Union and Concat are appropriate, as mentioned by others.

If you want a 'live' combination of the two lists (such that changes to the first and second list are automatically reflected in the 'combined' list) then you may want to look into Bindable LINQ or Obtics .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM