我有这个工作外连接查询:
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
。
答案 0 :(得分:0)
据我了解你可以致电
{
b.Title,
PublisherName = group.Any()?group.First().Name: "No publisher";
}
答案 1 :(得分:0)
我确实设法用以下方法解决了这个问题:
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)});