小编Aud*_*die的帖子

如何在Linq中添加两个列表,所以addedList [x] = listOne [x] + listTwo [x]?

我想添加两个数字类型的列表,使addList [x] = listOne [x] + listTwo [x]

列表的输出需要是Generic.IEnumerable,我可以在将来的linq查询中使用.

虽然我能够使用下面的代码来做到这一点,但我不禁觉得必须有更好的方法.有任何想法吗?

List<int> firstList = new List<int>(new int[] { 1, 3, 4, 2, 5, 7, 2, 5, 7, 8, 9, 0 });
List<int> secondList = new List<int>(new int[] { 4, 6, 8, 3, 1, 5, 9, 3, 0 });

int findex = 0;

ILookup<int, int> flookup = firstList.ToLookup(f =>
                            {
                               int i = findex;
                               findex++; 
                               return i;
                               }, p => p);  

var listsAdded = from grp in flookup
                 select grp.First() + secondList.ElementAtOrDefault(grp.Key); …
Run Code Online (Sandbox Code Playgroud)

.net c# linq

8
推荐指数
2
解决办法
6918
查看次数

标签 统计

.net ×1

c# ×1

linq ×1