How do I add two lists in Linq so addedList[x] = listOne[x] + listTwo[x]?

前端 未结 4 1714
我寻月下人不归
我寻月下人不归 2021-01-05 02:43

I want to add two lists of a numeric type such that addedList[x] = listOne[x] + listTwo[x]

The output of the list needs to be a Generic.IEnumerable that I can use i

4条回答
  •  失恋的感觉
    2021-01-05 03:23

    What you're looking for is a Zip method. This method allows you to combine to lists of equal length into a single list by applying a projection.

    For example

    var sumList = firstList.Zip(secondList, (x,y) => x + y).ToList();
    

    This method was added to the BCL in CLR 4.0 (Reference). It's fairly straight forward to implement though and many versions are available online that can be copied into a 2.0 or 3.5 application.

提交回复
热议问题