2

I have one entity which besides other information holds many images. This is one 2 many. In situation where I need to load just first from that collection to slow loading I have following query which retrieve collection of images.

 List<Entity> data = session.Query<Entity>()
          .Fetch(x=>x.Photos)//here I need only first element
          .Fetch(x=>x.Features)
          .ToList();
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
BobRock
  • 3,477
  • 3
  • 31
  • 48

2 Answers2

2

Make use of First() or FirstOrDefault() method of th linq wiil do task for you

List<Entity> data = session.Query<Entity>()
           .Fetch(x=>x.Photos.First())//
           .Fetch(x=>x.Features)
           .ToList(); 

or

List<Entity> data = session.Query<Entity>()
               .Fetch(x=>x.Photos.FirstOrDefault())//
               .Fetch(x=>x.Features)
               .ToList(); 

aslo read this before using this methods : When to use .First and when to use .FirstOrDefault with LINQ?

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1
List<Entity> data = session.Query<Entity>()
          .Fetch(x=>x.Photos.FirstOrDefualt())//here I need only first element
          .Fetch(x=>x.Features)
          .ToList();
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42