问题描述
我想合并2个不同的收藏.
示例集合1 :( linqpad tor 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和Conternatype
因此,我的目标是最终加入数据以获得像这样的结果集合
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个接下来要换取循环????
Collection 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; } }
推荐答案
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};
其他推荐答案
您可以通过以下方式实现预期结果:
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不是可确定的整数
问题描述
I am wanting to merge together 2 different collections.
Example collection 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();
Now is where I will query a another data source ( in memory for this question)
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" }, };
Things to notice 1. PersonID of 3197908 is in here 3 times BECAUSE they have a different ContactRoleTypeId and ContactType
So thus my GOAL is to end up joining the data to have result collection like this
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
I was trying to join
var ids = from azed in azedIdentity join personRole in personContactRoles on azed.PersonID equals personRole.PersonID select personRole;
I am thinking I need to have 2 nexted foreach loops ?????
Collection Poco model used for both sources is this:
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; } }
推荐答案
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};
其他推荐答案
You can achieve expected results with following:
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 });
however you will get 0 at ContactRoleTypeId since your POCO FindPersonContactViewModel.ContactRoleTypeId is not nullable integer