【发布时间】:2020-04-13 12:40:48
【问题描述】:
考虑 2 个具有相同架构的表:
var yesterday = new DataTable();
yesterday.Columns.Add("id", typeof(int));
yesterday.Columns.Add("part_number", typeof(string));
yesterday.Columns.Add("description", typeof(string));
yesterday.Columns.Add("comment", typeof(string));
yesterday.Columns.Add("information", typeof(string));
yesterday.Columns.Add("data", typeof(string));
var today = yesterday.Clone();
添加一些数据:
//yesterday data has 3 rows
yesterday.Rows.Add(1, "IVD_002", "IVD_002_RED", "Some comment", "Some information","Some data");
yesterday.Rows.Add(2, "IVD_003", "IVD_003_RED", "Some comment", "Some information", "Some data");
yesterday.Rows.Add(3, "IVD_004", "IVD_004_RED", "Some comment", "Some information", "Some data");
//today's data has the same 3 rows
today.Rows.Add(1, "IVD_002", "IVD_002_RED", "Some comment", "Some information", "Some data");
today.Rows.Add(2, "IVD_003", "IVD_003_RED", "Some comment", "Some information", "Some data");
today.Rows.Add(3, "IVD_004", "IVD_004_RED", "Some comment", "Some information", "Some data");
让我们添加更多数据:
//The New Row:
//In the output table I expect to see only the following row. The "id" column is 5 whereas in previous records there is no row with id = 5, part_number = IVD_002, description = IVD_002_RED
today.Rows.Add(5, "IVD_002", "IVD_002_RED", "Some comment", "Some information", "Some data");
//Another New Row:
//I dont expect to see this row in the result table because we are doing except only on "id","part_number","description" columns
today.Rows.Add(1, "IVD_002", "IVD_002_RED", "ROSES ARE RED", "PEANUTS", "=)");
我的目标是从“today”表中获取行,但“yesterday”表中的行除外,但只比较“id”、“part_number”、“description”列。
非常感谢纯 LINQ 解决方案,无循环。
【问题讨论】:
-
这个解决方案打算在 UiPath 中使用,所以我可以用代码做的事情相当有限。 LINQ 查询将是最佳选择。
-
@SalahAkbari 这是一个数据表,而不是自定义对象的列表。
-
@juharr 我相信它仍然是可能的,也许像
IEqualityComparer<DataRow>这样的东西,因为DataTable仍然是一个对象。