Allow filter include for non-collection properties

Open
#21,751 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
database

Research direction

Start with the issue's Organisation, Setting, and Location example and compare the existing collection filtered include with the requested conditional reference-property include. Done means EF Core can conditionally include Setting when its ApiKey matches, while preserving the existing collection behavior; no source files or tests are identified in the issue.

Written by the indexing model from the issue text.

Description

area-query customer-reported needs-design

Given the following set of classes

public class Organisation
{
	public Guid Id { get; set; }
	
	public string Name { get; set; }

	public List<Location> Locations { get; set; }

	public Setting Setting { get; set; }
}

public class Setting
{
	public string ApiKey { get; set; }
}

public class Location
{
	public Guid Id { get; set; }

	public Guid OrganisationId { get; set; }
	
	public Organisation Organisation { get; set; }
	
	public string Name { get; set; }
}

public class Db : DbContext
{
	public DbSet<Organisation> Organisations { get; set; }
}

I would like to Include the Setting when querying the organisations only if the ApiKey matches a certain string.
At the moment filter include only works for collections, but not for object properties.
I would like to have something like If(Predicate predicate) to include the Settings Property in the Organisations query, example:

using (var database = new Db())
{
	var organisations = 
            database
                .Organisations
		.Include(x => x.Locations.Where(l => l.Name.Contains("ABC"))) //This is possible now
		.Include(x => x.Setting.If(l => l.ApyKey.Contains("XYZ"))) // This is what I would like to do
		.Where(o => o.Name.Contains("OOO"))
		.ToArray();
}
Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.