I have the following join:
var simpleJoin = from b in books
join p in publishers on b.PublisherName equals p.Name
where p.Id == 1
select b;
What is the equivalent using the method syntax? I'm getting tripped up by the filter:
simpleJoin = books.Join(publishers, p => p.PublisherName, b => b.Name, (b, p) => b).Where(*can't access publishers here*)
Can I not use books
as my source collection? I'm wondering how we could manage filtering if we have multiple joins.
You'll need to include both b and p in the resultSelector
. For example, using an anonymous-typed object:
simpleJoin = books.Join(publishers, p => p.PublisherName, b => b.Name,
(b, p) => new { b = b, p = p })
.Where(result => result.p.Id == 1)
.Select(result => result.b);
You can filter the publishers
list prior to joining it to books
:
var simpleJoin = books.Join(publishers.Where(p => p.Id == 1),
b => b.PublisherName, p => p.Name, (b, p) => b);
仅作为示例-您也可以不使用join子句:
books.Where(b => publishers.Exists(p => p.Name == b.PublisherName && p.Id == 1));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.