连接集合和赋值
本文关键字:赋值 集合 连接 | 更新日期: 2023-09-27 17:49:18
我有两个集合,我想根据键属性连接它们,并将一个集合的值分配给另一个集合。我是这样做的
var joinedData = from collection_one in Office.Employees
join collection_two in NewOffice.Employees
on collection_1.OfficeId equals collection_two.OfficeId
select new { collection_one, collection_two};
// Declare a new Collection
ICollection<Office.Employees> updatedCollection = New List<Office.Employees>();
// Assign New Collection_Two Values to Collection_One
foreach (var item in joinedData.ToList())
{
item.collection_one.Deleted = item.collection_two.Deleted;
updatedCollection .Add(item.obp);
}
没有产生正确的结果。My Join产生的记录比内部连接产生的记录要多。有人能发现问题吗?
尝试使用DefaultIfEmpty():
var updatedCollection =
(from collection_one in Office.Employees
from collection_two in NewOffice.Employees.Where(x => x.OfficeId == collection_one.OfficeId).DefaultIfEmpty()
where collection_two == null
select new {
collection_one,
collection_two
}).Select(x => {
x.collection_one.Deleted = x.collection_two.Deleted;
return x.obp;
});
但是x.obp是什么?也许结果应该是x.collection_one?