Select inside Include in EF Core
See original GitHub issueI have an entity that looks like this (partially removed for brevity, it includes many other properties):
public class Tender
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string CreatorId { get; set; }
[ForeignKey("CreatorId")]
public virtual AppUser Creator { get; set; }
public ICollection<TenderCircle> TenderCircles { get; set; } = new List<TenderCircle>();
}
The TenderCircles
property is used to provide many-to-many relationship with another entity called Circle
. TenderCircle
entity looks like this:
public class TenderCircle
{
public int TenderId { get; set; }
[ForeignKey("TenderId")]
public Tender Tender { get; set; }
public int CircleId { get; set; }
[ForeignKey("CircleId")]
public Circle Circle { get; set; }
}
So, I’m doing the following query (partially removed for brevity, normally it includes many other Include
and ThenInclude
statements):
return _context.Tenders
.Include(t => t.Creator)
.Include(t => t.TenderCircles.Select(tc => new { CirlceId = tc.CircleId, TenderId = tc.TenderId }))
.ToList();
But, it simply doesn’t work. What I want to achieve is that I only want to get the TenderId
and CircleId
properties from TenderCircle
and ignore the actual Tender
and Circle
objects. How can I achieve this in EF Core?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Select inside Include in EF Core - entity framework
Now, I'm doing the following query (partially removed for brevity, normally it includes many other Include and ThenInclude statements):
Read more >Filtering Results Using Filtered Include Method in EF Core
In this article, we are going to show you how to use Filtered Include method in EF Core to filter results inside the...
Read more >Eager Loading of Related Data - EF Core
You can include related data from navigation defined only on a derived type using Include and ThenInclude . Given the following model: C#...
Read more >Querying in Entity Framework Core
Use the Include() method multiple times to load multiple navigation properties of the same entity. For example, the following code loads Grade and ......
Read more >Eager Loading using Include & ThenInclude in EF Core
We use the include & ThenInclude methods, along with the Projection Query in EF Core to load the related entities. In this tutorial,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@etairi Not currently, no. This is because the Tender entity is loaded, and the relationship from Tender to TenderCircles is loaded. Since the navigation property is part of a relationship that is being explicitly loaded it will always be set. The navigation property could be removed if it is not needed, but if the navigation exists and the relationship is loaded, then the navigation will be set.
@ajcvickers Okay. Thanks for info!