LINQ query syntax for selectmany + join + let
I want to compare a list of named data dictionaries with the actual data read from a data provider. The result should be a flat list of the form: [Table]: [Key] changed from [OldValue] to [NewValue].
I wanted to use linq query syntax, no performance needed. Dictionaries with the same name always have the same keys, no verification is required.
I came up with the following query (you can use it in LINQ-Pad), but I don't have access to table2 in the first connection. Error: "The name" table2 "is not available in the current context" (line 8).
Any ideas?
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; }
}
source to share
A junction is a search for matching items in two independent data sources. In other words, items on the right side of a join cannot depend on an item on the "current" left side of a join. Thus, you cannot join while you are flattening. You are simply to effectively isolate the "given two tables, find differences" part and then smooth out those results. I believe this will do what you want:
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;
source to share