用linq合并两个列表 [英] Combine two lists with linq

查看:70
本文介绍了用linq合并两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

    var myIds = new List<int>()
        {
            1, 2, 3
        };

    var yourIds = new List<int>()
        {
            2, 3, 4
        };

我如何将两个列表合并为一个.新列表如下所示:

How could I combine the two lists into one. The new list would look like this:

Id    Mine     Yours
---------------------
1      T         F
2      T         T
3      T         T
4      F         T

两者都包含整数并且仅在名称上有所不同.我曾考虑过创建另外两个具有代表每个列表名称的属性的列表,但是我敢肯定有更好的方法.

Both contain integers and vary only in name. I thought about creating two additional lists with a property that represented the name of each list, but I'm sure there is a better way.

推荐答案

myIds.Union(yourIds)
.Select(x => new 
             { 
                 Id = x,
                 Mine = myIds.Contains(x), 
                 Yours = yourIds.Contains(x)
             });

这篇关于用linq合并两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆