© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
41 replies
Pokey

❔ EF Core LINQ - Many to Many Search with && and ||

Hi there,

I have a 3 table structure for file storage that I would like to search, consisting of the following:

public class LabelEntity
{
  public uint Id { get; set; }
  public string Name { get; set; }
}

public class FileEntity
{
  public uint Id { get; set; }
  public string Md5Sum { get; set; }
  public string Name { get; set; }
  public string Location { get; set; }
}

public class FileLabelMappingEntity
{
  public uint Id { get; set; }
  public uint LabelId { get; set; }
  public LabelEntity Label { get; set; }
  public uint FileId { get; set; }
  public FileEntity File { get; set; }
}
public class LabelEntity
{
  public uint Id { get; set; }
  public string Name { get; set; }
}

public class FileEntity
{
  public uint Id { get; set; }
  public string Md5Sum { get; set; }
  public string Name { get; set; }
  public string Location { get; set; }
}

public class FileLabelMappingEntity
{
  public uint Id { get; set; }
  public uint LabelId { get; set; }
  public LabelEntity Label { get; set; }
  public uint FileId { get; set; }
  public FileEntity File { get; set; }
}


I would like to search files by assigned labels. I want to provide AND and OR groups for this search, so a search by
LabelEntity.Id
LabelEntity.Id
might look like so:

(1 || 2) && (5 || 7 || 12) && 17 && (4 || 6)
(1 || 2) && (5 || 7 || 12) && 17 && (4 || 6)


I have struggled for a while to work out an efficient way to do this. I will always have the IDs of the labels I want to search by as provided in the example, those are looked up beforehand.

I'd like to formulate this into a fast, efficient linq query(s)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

[EF Core/LINQ] Paginated Eager-Loaded One-to-Many
C#CC# / help
2y ago
❔ Help with EF Core Linq statement
C#CC# / help
4y ago
❔ EF Core duplicating many-to-many?
C#CC# / help
4y ago
❔ EF Core LINQ translation document
C#CC# / help
3y ago