Linq加入多个列不起作用

时间:2017-12-12 17:51:25

标签: c# linq join

我有一个LINQ联接,我正在处理多个列,但它没有正确加入。

我想基于他们的名字和他们的主电子邮件地址的价值加入两个表。

var batchId = 500000002;
var companyQuery = Companies 
    .Where(x => x.ImportCompany == null) // ImportCompany is a subtype of Company, i want to match on non-ImportCompany
    .Select(x => new
    {
        x.Id,
        x.Party.Name, //company is a subtype of Party
        x.Party.EmailAccounts.FirstOrDefault(e => e.IsPrimary).Address
    });

var importedQuery = ImportCompanies
    .Where(x => x.ImportBatch_Id == batchId)
    .Select(imported => new
    {
        imported.Id,
        imported.Company.Party.Name,
        imported.Company.Party.EmailAccounts.FirstOrDefault(e => e.IsPrimary).Address
    });

var query = importedQuery
    .Join(companyQuery, x => new { x.Name, x.Address }, x => new { x.Name, x.Address }, 
         (imported, existing) => new { Imported = imported.Id, Existing = existing.Id })
    .ToList();

query没有返回结果。

我的价值设置如下:

ImportedCompany Record | Company Record
---------------------------------------
Name = "Test"          | Name = "Test"
Address = null         | Address = null

为什么我的加入没有返回任何结果?是否有一些古怪的地址,或者是因为地址在子表中,某些东西搞砸了?

1 个答案:

答案 0 :(得分:0)

我通过更改此行来修复此问题

copyToClipboard()

到这个

x.Party.EmailAccounts.FirstOrDefault(e => e.IsPrimary).Address