C#C
C#3y ago
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; }
}


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 might look like so:

(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)
Was this page helpful?