0

I have been sifting through several posts but none of the answers seems to work or to be applicable in my case, maybe I am overseeing something ...

I have an Entity Yogi which have a Collection of Subscriptions (also an Entity). I want to retrieve a Yogi but including only the Subscriptions after a specific date.

I tried the following (also replaced the (2nd) Where with a Select):

ctx.Yogis.Where(y => y.YogiId == id)
         .Include(y => y.Subscriptions.Where(s=> s.YogaClassDate.Date >= DateTime.Now.Date))
         .Include("Subscriptions.YogaClass")
         .FirstOrDefault();

but received this error as an result:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

I want to avoid if possible to have to iterate and compare afterwards so is there any way of getting this directly or as others have suggested, no there isn't and I should get a collection of all Subscriptions for that Yogi and filter out the ones I don't want to show?

Thank you in advance for any suggestions or directions!

EDIT (THANK YOU GERT ARNOLD): I tried the anonymous projection but I missed a crucial part

.AsEnumerable()
.Select(x => x.b)

So this made it work:

ctx.Yogis.Where(y => y.YogiId == id)
         .Select(y => new
                      {
                       y,
                       Subscriptions = y.Subscriptions.Where(s=> s.YogaClassDate >= DateTime.Now)
                      })
         .AsEnumerable()
         .Select(x => x.y)
         .ToList();
Dimitri
  • 1,185
  • 2
  • 15
  • 37

1 Answers1

0

try it:

    ctx.Yogis.Where(y => y.YogiId == id)
     .Include(y => y.Subscriptions)
     .Include("Subscriptions.YogaClass").Where(s=> s.Subscriptions.YogaClassDate.Date >= DateTime.Now.Date)
     .FirstOrDefault();
Mahdi Anjam
  • 145
  • 1
  • 17