首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将来自2个数据源的2个集合列表连接在一起,查找匹配项并遍历结果

将来自2个数据源的2个集合列表连接在一起,查找匹配项并遍历结果
EN

Stack Overflow用户
提问于 2019-06-20 08:22:33
回答 2查看 53关注 0票数 0

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

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

代码语言:javascript
复制
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();

现在我将查询另一个数据源(在内存中查询这个问题)

代码语言:javascript
复制
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

因此,我的目标是最终连接数据以获得如下结果集合

代码语言:javascript
复制
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

我试着加入

代码语言:javascript
复制
 var ids = from azed in azedIdentity
                join personRole in personContactRoles on azed.PersonID equals personRole.PersonID
                select personRole;

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

用于两个来源的集合Poco模型如下:

代码语言:javascript
复制
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; }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-20 09:48:34

代码语言:javascript
复制
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};
票数 0
EN

Stack Overflow用户

发布于 2019-06-20 09:33:07

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

代码语言:javascript
复制
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不能为空整数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56677478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档