2

I have this piece of code.

return Folder.GetAllWithInclude(x => x.SubFolder).Take(5);

This code returns 5 Folder items. What I want to do is limit Subfolder to 5 instead of limiting Folder to 5. I have tried the following

return Folder.GetAllWithInclude(x => x.SubFolder.Take(5));

but it doesn't seem to do the trick.

I might be missing the proper syntax here.

Thank you so much in advance!

  • Without knowing what `GetAllWithInclude` returns precisely, there might be better options, but in theory the following should work: `return Folder.GetAllWithInclude(x => x.SubFolder).SelectMany(s=>s).Take(5);` – Me.Name Jun 28 '16 at 09:24
  • Hi @Me.Name but I still have to return the complete Folder records together with its 5 Subfolder items. – Jack Frother Jun 28 '16 at 09:28
  • Ah right. And if the first folder contains 3 items and a second 5, it should return a folder with 3 and a folder with 2 items? It would seem that would be easier to implement in the `GetAllWithInclude` method. Is that a custom method or some library method I'm unaware of? – Me.Name Jun 28 '16 at 09:34
  • Ohhhh I'm sorry its a custom method lets say we have 2 Folders, the #1 folder contains 6 subfolders and #2 folder contains 2 subfolders then the code should return 2folders with the #1 folder containing only 5 subfolders while #2 folder still contains 2 subfolders. – Jack Frother Jun 28 '16 at 09:35
  • No problem, but could you include the method in your post? Perhaps the method itself could be expanded with an optional parameter to restrict the maximum subfolders (or a dynamic criterium). If it can't be implemented in the GetAllWithInclude method, the alternatives would depend on what the object returned by GetAllWithInclude looks like. – Me.Name Jun 28 '16 at 09:45

1 Answers1

2

There's no method for Include(Where Expression). If you are using Include you will always load all the records.

Update

you can use Projection for this problem

Folder.Select(F => new
        {
            FolderName = F.FolderName,
            SubFolders = F.SubFolders.Take(5)
        }).ToList().Select(F => new Folder()
        {
            FolderName = F.FolderName,
            SubFolders = F.SubFolders
        };
Kahbazi
  • 14,331
  • 3
  • 45
  • 76