question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select inside Include in EF Core

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Aug 30, 2018

@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.

0reactions
etairicommented, Aug 31, 2018

@ajcvickers Okay. Thanks for info!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found