0

Suppose I have 3 entities Country , State and City .And Relationship is City->State->Country. Now I have query for Search like

 return container.Countries.Include("States.Cities")
.Where(c=>c.States.Sum(s.Population)>10000)
.Search(filter)        
.Skip(startIndex)    
.Take(itemCount)
.ToList();

I am Including Cities to all States in above query , But I want to Include it at the end of Query that is after Take Method and before ToList method.Means Suppose query return 50 records I want to Include Cities to State only for that 50 records not for all States as in above query. Can someOne help how i Can do that ? Any help will be highly appericiable. This is just dummy query and I want to do this for Performance point of view. Thanks in advance

yo chauhan
  • 12,079
  • 4
  • 39
  • 58

2 Answers2

1

Are you forget to import the System.Data.Entity namespace for your code? Look this

How to use Include() after OfType()?

Any way, I think it's all right for your code. ALL the methods before ToList will return IQueryable or something like that. That means it's lazy evaluation. So whatever the calling order of the methods, you'll get the same performance.

Community
  • 1
  • 1
Kirin Yao
  • 1,606
  • 2
  • 14
  • 21
1

There's no difference where you put Include in you query. It is only a behavior modifier, it does not get executed at the moment you see it. As long as you operate on IQueryable Include will have same effect wherever you put it.

Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35