我想比较一个命名数据字典列表与从数据提供者读取的实际数据.结果应该是以下形式的平面列表:[表]:[Key]从[OldValue]更改为[NewValue].
我想使用linq的查询语法,不需要性能.具有相同名称的词典总是具有相同的键,不需要检查.
我提出了以下查询(您可以在LINQ-Pad中使用它)但我在第一次连接中无法访问table2.错误:"名称'table2'在当前上下文中不可用"(第8行).
有任何想法吗?
void Main()
{
var db1 = new[] { new Table { Name = "TableA", Data = new Dictionary<string, string> { { "KeyA", "000" } } } };
var changes = from table1 in db1
let table2 = ReadTable(table1.Name)
from row1 in table1.Data
join row2 in table2.Data
on row1.Key equals row2.Key
where !row1.Value.Equals(row2.Value)
select new { Table = table1.Name, Key = row1.Key, From = row1.Value, To = row2.Value };
changes.Dump();
}
Table ReadTable(string Name)
{
return new Table { Name = "TableA", Data = new Dictionary<string, string> { { "KeyA", "111" } } };
}
class Table
{
public string Name { get; set; }
public Dictionary<string, string> Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
连接点是在两个独立的数据源中查找匹配元素.换句话说,连接右侧的元素不能依赖于连接的"当前"左侧的元素.因此,当你扁平化时,你无法加入.您只需要有效地隔离"给定两个表,找到差异"部分,然后将这些结果展平.我相信这会做你想要的:
var changes = from table1 in db1
let table2 = ReadTable(table1.Name)
from change in
(from row1 in table1.Data
join row2 in table2.Data
on row1.Key equals row2.Key
where !row1.Value.Equals(row2.Value)
select new
{
Table = table1.Name,
Key = row1.Key,
From = row1.Value,
To = row2.Value
})
select change;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
338 次 |
| 最近记录: |