我正在尝试将两个表中的结果合并到一个列表中,然后将其显示在字典中,其中键是域,值是两个表中的URL列表。
class Source1Entity { string Domain {get;} string PageUrl {get;} /* more properties */ }
class Source2Entity { string Domain {get;} string PageUrl {get;} /* more properties */ }
我到目前为止:
Dictionary<string, List<string>> results =
from firstSource in context.Source1
join secondSource in context.Source2 on firstSource.Domain equals secondSource.Domain
group firstSource by firstSource.Domain into g
...
答案 0 :(得分:1)
目前还不完全清楚,但我怀疑你实际上想要将这两个表视为等价物 - 所以你可以投射到一个公共表单,然后使用Concat
,然后调用{{ 1}}:
ToLookup
然后:
var projectedSource1 = context.Source1.Select(x => new { x.Domain, x.PageUrl });
var projectedSource2 = context.Source2.Select(x => new { x.Domain, x.PageUrl });
var results = projectedSource1
.Concat(projectedSource2)
.ToLookup(x => x.Domain, x => x.PageUrl);
或
// For a particular domain - you'll get an empty sequence it the
// domain isn't represented
foreach (var url in results[domain])
虽然可以使用foreach (var entry in results)
{
Console.WriteLine("Domain: {0}", entry.Key);
foreach (var url in entry)
{
Console.WriteLine(" Url: {0}", url);
}
}
,但Dictionary
通常更适合单键多值查询。
答案 1 :(得分:0)
你需要一个GroupJoin。 试试这个......
join secondSource in context.Source on firstSource.Domain equals secondSource.Domain into group_join