Reputation: 85
I want to launch a query crossing two tables that are related from many to many and in entity framework core 5, it is not necessary to create the c# class of the intermediate table. So with include I launch the query with left join which is correct, but I need to launch an inner join and I can't because I don't have the c# class of the intermediate table. How would it be possible to do this without having to create the c# class of the intermediate table?
thank you very much
Upvotes: 1
Views: 1053
Reputation: 205849
You don't need intermediate entity in order to achieve inner join in a LINQ to Entities query. All you need is SelectMany
without DefaultIfEmpty()
.
For instance, using the sample model from Many-to-many section of the documentation:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public string TagId { get; set; }
public ICollection<Post> Posts { get; set; }
}
the following will generate inner join:
var query = from post in db.Posts
from tag in post.Tags
select new { post, tag };
or with method syntax
var query = db.Posts
.SelectMany(post => post.Tags, (post, tag) => new { post, tag });
Upvotes: 1