根据列取两个列表的并集 [英] Taking union of two lists based on column

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

问题描述

我正在使用Linq to Sql合并两个列表.使用List1和List2:

I am taking a union of two lists using Linq to Sql. Using List1 and List2:

 var tr = List1.Union(List2).ToList();

联盟工作正常,但问题是它正在检查每一列并删除我想要的某些行.所以我想知道是否有一种方法可以仅基于每个列表的一个列(例如,假设id)执行联合?

Union works fine, but the problem is it is checking each column and removes some of the rows that I want. So I was wondering if there is a a way I can perform a union based on one column only, like let's say id, of each list?

类似的东西:

var t = List1.id.Union(List2.id).ToList();

这行不通,但是我想知道是否可以通过LINQ或T-SQL做到这一点

This doesn't work, but I was wondering if there is a way to do this, either with LINQ or T-SQL

推荐答案

您应使用此

You should use this Union() overload (with a custom equality comparer) , or something like this:

list1.Concat(list2).GroupBy(x => x.DateProperty).Select(m => m.First());

第一个解决方案肯定更有效.

The first solution is certainly more efficient.

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

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