开发者

How do join two lists without have same fields using linq?

开发者 https://www.devze.com 2023-01-31 02:08 出处:网络
Class sam { public void m1() { List<int> A = new List<int>() {1,2,3}; List<int> B = new List<int>() {4,5,6};
Class sam
{
    public void m1()
    {
        List<int> A = new List<int>() {1,2,3};
        List<int> B = new List<int>() {4,5,6};

        for (int i = 0; i < A.count; i++)
        {
            c.add(m2(A[i], B[i]));
        }
    }

    public int M2(int a, int b)
    {
        return a + b;
    }
}

In this program i retrive from two lists and pass aruguments. I like开发者_如何学编程 call the method m2 in linQ


It's unclear from the question, but perhaps you mean:

var C = A.Zip(B, (a, b) => a + b)
         .ToList();

If you want to hand over the addition to your M2 method, you could do:

// C# 4
var C = A.Zip(B, M2); 

// C# 3 (not likely since Zip was introduced in .NET 4)
var C = A.Zip(B, (a, b) => M2(a, b)); 
var C = A.Zip<int, int, int>(B, M2);
0

精彩评论

暂无评论...
验证码 换一张
取 消