0

Say I have the following model -

public class Folder
{
   public int Id {get; set;}

   public virtual List<Letter> Letters {get; set;
}

public class Letter
{
   public int Id {get; set;}

   public virtual list<Folder> Folders;

   // ... 
}

What query should I use to get all letters from a specific folder?

For instance if I want all the letters in folder with Id 1:

If I simply use context.Folders.Find(1).Letters, it gets me a List instead of an IQueryable<Letter>, and it gets me some data that is not useful for this specific case.

However if I use context.Letters.Where(x => x.Folders.Any(f => f.Id == 1)

I'm inefficiently iterating through all the Letters which seems bad.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 1
    There are many ways to do that, all more or less result in the same queries to database. For example: context.Folders.Where(c => c.Id == 1).SelectMany(c => c.Letters); – Evk May 08 '16 at 12:31
  • Exactly what I needed. Thanks alot –  May 08 '16 at 13:43

0 Answers0