我有List<object>
和List<string>
的记录。我希望内部加入两个列表。
以下是我的记录列表。
var listContributionDetailsToBeUpdated = new List<ContributionDetailModel>
{
new ContributionDetailModel { EmployeeNumber="1", EmployeeFirstName="david", EmployeeLastName="k", NHIPNumber="nhi-100" },
new ContributionDetailModel { EmployeeNumber="2", EmployeeFirstName="xavior", EmployeeLastName="f",NHIPNumber="nhi-101"},
new ContributionDetailModel { EmployeeNumber="3", EmployeeFirstName="george", EmployeeLastName="s", NHIPNumber="nhi-102" },
new ContributionDetailModel { EmployeeNumber="4", EmployeeFirstName="pandit", EmployeeLastName="h",NHIPNumber="nhi-103"},
new ContributionDetailModel { EmployeeNumber="5", EmployeeFirstName="zania", EmployeeLastName="d", NHIPNumber="nhi-104" }
};
var updatedRecored = new List<string> { "nhi-100", "nhi-101", "nhi-102" };
任何人都可以帮我加入两个列表。
答案 0 :(得分:3)
这很直截了当。只需从贡献详细信息中选择NHIPNumber
作为加入的关键:
var result = from c in listContributionDetailsToBeUpdated
join r in updatedRecored on c.NHIPNumber equals r
select c;
方法语法:
var result = listContributionDetailsToBeUpdated
.Join(updatedRecored, c => c.NHIPNumber, r => r, (c, r) => c);
您也可以使用过滤而不是加入,但这不是最佳方式,因为join是一个set操作,它使用内部查找连接集合。使用简单过滤,您将具有复杂度O(N * M)而不是O(N + M)
var result = listContributionDetailsToBeUpdated
.Where(c => updatedRecored.Contains(c.NHIPNumber));
答案 1 :(得分:0)
以下内容也可能有效:
var result = listContributionDetailsToBeUpdated.Where(x => updatedRecored.Contains(x.NHIPNumber));