我有此工作的外部加入查询:
var outerJoin = from b in books
join p in publishers on b.PublisherName equals p.Name into joinedPublishers
from publisher in joinedPublishers.DefaultIfEmpty()
select new { b.Title, PublisherName = (publisher == null ? "No publisher" : publisher.Name) };
我正在尝试使用方法语法产生相同的内容。到目前为止,我有:
outerJoin = books.GroupJoin(publishers, b => b.PublisherName, p => p.Name, (b, group) => new { b.Title, PublisherName = group.DefaultIfEmpty()});
但是,当我需要出版商名称的字符串时,group
是IEnumerable
。
据我了解,您可以致电
{
b.Title,
PublisherName = group.Any()?group.First().Name: "No publisher";
}
我确实设法通过以下内容弄清楚了:
outerJoin = books.GroupJoin(publishers,
b => b.PublisherName,
p => p.Name,
(b, publisherGroup) => new { b.Title, publisherGroup})
.SelectMany(x => x.publisherGroup.DefaultIfEmpty(),
(x, y) => new {x.Title, PublisherName = (y == null ? "No publisher" : y.Name)});