我有班级对象列表"数据"看起来像:
class Data
{
int code;
string name;
...
DateTime date_update;
}
我有另一个班级列表,如:
class RefCodes
{
int old_code;
int new_code;
string new_name;
DateTime date_update;
}
"数据"的列表包含1,000个对象。 " RefCodes"的列表包含30个对象。
我需要替换列表"数据", 田野: "代码"与" new_code"的价值一致, 和#34;名称"具有" new_name"。
的值替换需要仅针对其代码存在于列表中的对象" RefCodes"。
通过查询:如果Data.code == RefCodes.old_code
中的代码
我该怎么办?
答案 0 :(得分:1)
我认为你正在寻找这个:
foreach (var rcodeObj in RefCode)
{
foreach(var obj in (Data.Where(t => t.code == rcodeObj.old_code)))
{
obj.code = rcodeObj.new_code;
obj.name = rcodeObj.new_name;
}
}
答案 1 :(得分:1)
您可以使用以下代码:
foreach (var x in DataList)
{
var itemRefCode = RefCodesList.FirstOrDefault(d => d.old_code == x.code);
if (itemRefCode != null)
{
x.code = itemRefCode.new_code;
x.name = itemRefCode.new_name;
}
}
答案 2 :(得分:1)
如果你使用的是C#6,你可以使用linq做这样的事情
var updatedData = data.Select(x => new Data
{
code = refCodes.FirstOrDefault(y => y.old_code == x.code)?.new_code ?? x.code,
name = refCodes.FirstOrDefault(y => y.old_code == x.code)?.new_name ?? x.name,
});
答案 3 :(得分:1)
您可以遍历每个列表并按如下方式更新值。这里我使用了一些示例输入,如下所示。请注意,为简单起见,我正在考虑将这些类的字段公开:
List<Data> dataList = new List<Data>
{
new Data { code = 1, name = "A" },
new Data { code = 2, name = "B" },
new Data { code = 10, name = "C" },
};
List<RefCodes> refList = new List<RefCodes>
{
new RefCodes { old_code = 1, new_code = 11, new_name = "X" },
new RefCodes { old_code = 2, new_code = 22, new_name = "Y" }
};
Console.WriteLine("Before");
dataList.ForEach(data => Console.WriteLine(data.code + ": " + data.name));
Console.WriteLine("");
以下是进行更新的代码:
foreach (var refCodes in refList)
{
foreach (var data in dataList)
{
if (data.code == refCodes.old_code)
{
data.code = refCodes.new_code;
data.name = refCodes.new_name;
}
}
}
Console.WriteLine("After");
dataList.ForEach(data => Console.WriteLine(data.code + ": " + data.name));
输出:
Before
1: A
2: B
10: C
After
11: X
22: Y
10: C
答案 4 :(得分:1)
您需要的是Left Outer Join。 例如,
IEnumerable<Data> query = from data in dataList
join refCode in refList on data.code equals refCode.old_code into joined
from subCode in joined.DefaultIfEmpty()
select new Data
{
code = subCode?.new_code ?? data.code,
name = subCode?.new_name ?? data.name,
date_update = subCode == null ? data.date_update : DateTime.Now
};
将返回一个包含您期望结果的序列。
答案 5 :(得分:1)
这会解决您的问题:
public void Update( List<Data> data, List<RefCodes> refCodes )
{
List<RefCodes> differences = refCodes
.Where( r => data.Any( d => r.old_code == d.code ) )
.ToList();
differences.ForEach( ( RefCodes item ) =>
{
Data element = data.FirstOrDefault( d => d.code == item.old_code );
element.code = item.new_code;
element.name = item.new_name;
} );
}
答案 6 :(得分:0)
**让我们说tempAllocationR是列表1,而tempAllocationV是List2 **
var tempAllocation = new List<Object>();
if (tempAllocationR.Count > 0 && tempAllocationV.Count > 0)
{
foreach (TempAllocation tv in tempAllocationV)
{
var rec = tempAllocationR.FirstOrDefault(tr => tr.TERR_ID == tv.TERR_ID && tr.TERR == tv.TERR && tr.Team == tv.Team);
if (rec != null)
{
rec.Vyzulta = tv.Vyzulta;
}
else
{
tempAllocationR.Add(tv);
}
}
tempAllocation = tempAllocationR;
}
else if (tempAllocationV.Count == 0 && tempAllocationR.Count > 0)
{
tempAllocation = tempAllocationR;
}
else if (tempAllocationR.Count == 0 && tempAllocationV.Count > 0)
{
tempAllocation = tempAllocationV;
}