how to join two Lists in linq
i have two List A,B which consists integer values ,list A contains 40 to 1 list B contains 40 to 79 i like to both values except 40 and st开发者_JAVA百科ore it in another list using Linq.The resultant list like this {80,80...}. how can i do this? Is it possible to do this?
It sounds like you're trying to "join" these in a pairwise fashion by index: the first element from each list, then the second element etc. That suggests you want Zip
, which was introduced in .NET 4:
var zipped = list1.Zip(list2, (x1, x2) => x1 + x2);
If you're using .NET 3.5, you can use a separate implementation of the same method, such as the one in MoreLINQ.
EDIT: Alternatively, Eric Lippert posted some source code for Zip
a while ago, too.
Check out the IEnumerable<T>.Join()
method.
using System;
using System.Linq;
class Program
{
static void Main()
{
// Two source arrays.
var array1 = new int[] { 1, 2, 3, 4, 5 };
var array2 = new int[] { 6, 7, 8, 9, 10 };
// Add elements at each position together.
var zip = array1.Zip(array2, (a, b) => (a + b));
// Look at results.
foreach (var value in zip)
{
Console.WriteLine(value);
}
}
}
--- Output of the program ---
7 9 11 13 15
Try Joining them together
http://weblogs.asp.net/rajbk/archive/2010/03/12/joins-in-linq-to-sql.aspx
http://msdn.microsoft.com/en-us/library/bb397676.aspx
精彩评论