[英]C# compare two Lists and update it's column values
I've got two C# lists (List listA and List listB) how can I compare these two and if duplicate (of specific columns ex. ID_num and ID_cust) is found then update column "ID_duplicate" which is value of listB's columns ID. 我有两个C#列表(列表listA和列表listB),如何比较这两个列表,如果发现重复(特定列,例如ID_num和ID_cust),则更新列“ ID_duplicate”,它是listB列ID的值。
DataSet ds = subMain;
List<string> listA = (from r in ds.Tables[0].AsEnumerable()
Select r.Field<string>("ID_num") +
r.Field<string>("ID_cust")).ToList();
DataSet dsMain = Mains;
List<string> listB = (from r in dsMain.Tables[0].AsEnumerable()
select r.Field<string>("ID_num") +
r.Field<string>("ID_cust")).ToList();
I want that listA
will contain new column ID_duplicate
with value ID_num
from listB
. 我想这
listA
将包含新列ID_duplicate
与价值ID_num
从listB
。
So that duplicates will be somehow linked with this ID_num
. 这样,重复项将以某种方式与此
ID_num
链接。
I will then update that ID_duplicate
to database. 然后,我将该
ID_duplicate
更新为数据库。
Edit: Added more explanation in comment bellow. 编辑:在注释下面添加了更多解释。
If I understand is a join: 如果我了解是加入:
var listA = new List<Row> {
new Row { ID= 1, IdNum = 1, IdCust = 1 },
new Row { ID= 2, IdNum = 1, IdCust = 2 },
new Row { ID= 3, IdNum = 2, IdCust = 1 },
new Row { ID= 4, IdNum = 1, IdCust = 3 },
new Row { ID= 5, IdNum = 3, IdCust = 1 },
new Row { ID= 6, IdNum = 4, IdCust = 1 }
};
var listB = new List<Row> {
new Row { ID= 1, IdNum = 5, IdCust = 1 },
new Row { ID= 5, IdNum = 6, IdCust = 2 },
new Row { ID= 7, IdNum = 2, IdCust = 1 },
new Row { ID= 9, IdNum = 1, IdCust = 3 },
new Row { ID= 11, IdNum = 7, IdCust = 2 }
};
var t = (from a in listA
join b in listB on a.IdCust.ToString() + a.IdNum.ToString()
equals
b.IdCust.ToString() + b.IdNum.ToString()
select new
{
ID = a.ID,
IdUpdate = b.ID
}).ToArray();
foreach (var item in t)
{
Console.WriteLine("ID {0} IdUpdate {1}", item.ID, item.IdUpdate);
}
Here the Row class 这是Row类
class Row
{
public int ID { get; set; }
public int IdNum { get; set; }
public int IdCust { get; set; }
}
Obviusly you can create a calculated column on Row class like this 显然,您可以像这样在Row类上创建一个计算列
public string ValueToCompare
{
get
{
return this.IdNum.ToString() + this.IdCust.ToString();
}
}
and use this on join for comparison 并在连接时使用它进行比较
Max 最高
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.