【问题标题】:Join 2 collection list together from 2 data sources find the matches and loop through the results从 2 个数据源将 2 个集合列表连接在一起,找到匹配项并循环遍历结果
【发布时间】:2019-11-02 18:32:26
【问题描述】:

我想将 2 个不同的集合合并在一起。

示例合集1:(linqpad to live sql server)

Sample Data (collection azedIdentity):

PersonID | FirstName | LastName   |  
 3197908    John        Smith
 4444       Jody        Smith
 55555      Jon         Smither



var azedIdentity = PersonMatchNoDOBRequired("John", "Smith").AsDynamic()
    .Select (x => new FindPersonContactViewModel
    {
        PersonID = x.PersonID,
        AZEDID = x.AZEDID,
        FirstName = x.FirstName,
        MiddleName = x.MiddleName,
        LastName = x.LastName,
    }).ToList();

现在是我要查询另一个数据源的地方(这个问题在内存中)

var personContactRoles = new List<FindPersonContactViewModel>()
{ new FindPersonContactViewModel { PersonID = 3197908, FirstName = "John", MiddleName= "", LastName="Smith", ContactType="Farmer", ContactRoleTypeId = 1, ExistInContactManager = true, ActionType = true, IsInContactManager = true },
  new FindPersonContactViewModel { PersonID = 3197908, FirstName = "John", MiddleName= "", LastName="Smith", ContactType="Plumber", ContactRoleTypeId = 2, ExistInContactManager = true, ActionType = true, IsInContactManager = true },
  new FindPersonContactViewModel { PersonID = 3197908, FirstName = "John", MiddleName= "", LastName="Smith", ContactType="Landscaper", ContactRoleTypeId = 3, ExistInContactManager = true, ActionType = true, IsInContactManager = true },
  new FindPersonContactViewModel { PersonID = 2, FirstName = "Jon", MiddleName= "", LastName="Smither" },
  new FindPersonContactViewModel { PersonID = 4, FirstName = "Jo", MiddleName= "", LastName="Smith" },
  new FindPersonContactViewModel { PersonID = 5, FirstName = "Jody", MiddleName= "", LastName="Smith" },
  new FindPersonContactViewModel { PersonID = 6, FirstName = "Johnn", MiddleName= "", LastName="Smith" },
  new FindPersonContactViewModel { PersonID = 7, FirstName = "Jake", MiddleName= "", LastName="Smith" },
  new FindPersonContactViewModel { PersonID = 8, FirstName = "Jock", MiddleName= "", LastName="Smith" },
};

注意事项 1. 3197908 的 PersonID 在这里出现了 3 次,因为它们有不同的 ContactRoleTypeId 和 ContactType

因此,我的目标是最终加入数据以收集这样的结果

PersonID | FirstName | LastName   |  ContactRoleTypeId  | ContactType
 3197908    John        Smith                1                Farmer
 3197908    John        Smith                2                Plumber
 3197908    John        Smith                3                Landscaper
 4444       Jody        Smith
 55555      Jon         Smither

我正在尝试加入

 var ids = from azed in azedIdentity
                join personRole in personContactRoles on azed.PersonID equals personRole.PersonID
                select personRole;

我想我需要有 2 个下一个 foreach 循环?????

用于两个来源的收集 Poco 模型是这样的:

public class FindPersonContactViewModel
{

    public int PersonID { get; set; }
    public string AZEDID { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public int? GenderTypeId { get; set; }
    public string DOB { get; set; }
    public int ContactRoleTypeId { get; set; }
    public string ContactType { get; set; }
    public int PersonTypeId { get; set; }
    public string PreferredPhone { get; set; }
    public string PreferredEmail { get; set; }
    public string PhysicalAddress { get; set; }
    public bool ExistInContactManager { get; set; }
    public bool ActionType { get; set; }
    public bool IsInContactManager { get; set; }
}

【问题讨论】:

    标签: c# .net list linq collections


    【解决方案1】:
    var result = from azed in azedIdentity join personRole in personContactRoles on azed.PersonID equals personRole.PersonID 
              into r1 from p in r1.DefaultIfEmpty() select 
            new FindPersonContactViewModel{PersonID = azed.PersonID, FirstName = azed.FirstName, LastName = azed.LastName,
                                          ContactRoleTypeId = p == null ? 0 : p.ContactRoleTypeId, ContactType = p == null ? "" : p.ContactType};
    

    【讨论】:

    • 非常酷。 1. 如果我还想更新/设置 ExistInContactManager 为 True 如果匹配,这是匹配的 3 条记录 2. 如果我最终还需要更新所有电子邮件地址与第三个数据连接拉...一个for循环遍历记录?提前谢谢
    【解决方案2】:

    您可以通过以下方式达到预期的效果:

    var results = personContactRoles.Join(
        azedIdentity,
        x => x.FirstName + x.LastName,
        y => y.FirstName + y.LastName,
        (x, y) => new FindPersonContactViewModel()
        {
            PersonID = y.PersonID,
            FirstName = y.FirstName,
            LastName = y.LastName,
            ContactRoleTypeId = x.ContactRoleTypeId,
            ContactType = x.ContactType
        });
    

    但是你会在ContactRoleTypeId 得到0,因为你的POCO FindPersonContactViewModel.ContactRoleTypeId 不是可为空的整数

    【讨论】:

    • 好吧,我想要的是所有不匹配的记录,以及 3 个匹配项,有点像 concat 方法,但删除 1 条记录,因为它只在 azedIdentity 中。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2013-07-20
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多