SQL Linq与多个列相交

时间:2012-09-28 08:50:21

标签: c# linq

我有2个列表

A = 74137 records (7 columns)
B = 63029 records (5 columns)

并希望找到所有

的记录
A.column1 = B.column1 and A.column2 = B.column2 and A.column3 = B.column3

但想要返回A的整个7列。 到目前为止,我尝试了这段代码:

var ListCucolumn3uriPostale = (from A in _TempListaAdreseInProcesare.AsParallel()
                                           join B in tblcolumn3uri.AsParallel()
                                           on
                                           new { column1 = A.column1.ToUpper(), column2 = A.column2.ToUpper(), column3 = A.column3 }
                                           equals
                                           new { column1 = B.column1.ToUpper(), column2 = B.column2.ToUpper(), column3 = B.column3 }
                                           select new TempSpecificatii
                                            {
                                                column0 = A.column0,
                                                column4 = A.column4,
                                                column1 = B.column1,
                                                column2 = B.column2,
                                                column3 = B.column3,
                                                column5 = B.column5,
                                                column6 = B.column4,
                                                column7 = A.column7,
                                                column8 = A.column8,
                                                column9 = A.column9,
                                                column10 = A.column10
                                            }).ToList();

但我得到了一份186395记录,这是不正常的。我改变了这样的代码:

var listacucoduripostale = (from A in _TempListaAdreseInProcesare.AsParallel()
                            select new
                            {
                                column1 = A.column1.ToUpper(), column2 = A.column2.ToUpper(), column3 = A.column3
                            }).Intersect(
                           from B in tblCoduri.AsParallel()
                           select new
                           {
                               column1 = B.column1.ToUpper(), column2 = B.column2.ToUpper(), column3 = B.column3
                           }).ToList();

我得到了23567条记录的良好结果,但在列表中只有3列,我想要来自A的所有列。我必须改变以获得所需的结果。 感谢。

1 个答案:

答案 0 :(得分:1)

我很想这样做,HashSet应该快速查找。

var bLookupSet = new HashSet(b.Select(b =>
                   new { 
                       Column1 = b.Column1, 
                       Column2 = b.Column2,
                       Column3 = b.Column3
                       }));

var resultList = a.Where(a => bLookupSet.Contains(
                   new { 
                       Column1 = a.Column1, 
                       Column2 = a.Column2,
                       Column3 = a.Column3
                       })).ToList();

如果你需要匹配不区分大小写,并且所有3列都是字符串,那么我会这样做,

var bLookupSet = new HashSet(b.Select(b =>
                   new { 
                       Column1 = b.Column1.ToLower(), 
                       Column2 = b.Column2.ToLower(),
                       Column3 = b.Column3.ToLower()
                       }));

var resultList = a.Where(a => bLookupSet.Contains(
                   new { 
                       Column1 = a.Column1.ToLower(), 
                       Column2 = a.Column2.ToLower(),
                       Column3 = a.Column3.ToLower()
                       })).ToList();