Search code examples
linqentity-frameworkentity-framework-4wcf-data-serviceslinqpad

3rd level filter with LINQ in WCF Data Services (LINQPad)


Hi guys maybe I write this the wrong way but can't make it work.

I got the following Entity Model: [Division]<0..1>--<*>[Session]<1>--<0..1>[Film]

I run the following LINQ query (in LINQPad):

from d in Divisions select new {d, d.Sessions, 
films = from s in d.Sessions where s.Film.Title !=null select s}

but I get the error:

Constructing or initializing instances of the type <>f__AnonymousType03[LINQPad.User.Division,System.Collections.ObjectModel.Collection1[LINQPad.User.Session],System.Collections.Generic.IEnumerable`1[LINQPad.User.Session]] with the expression d.Sessions.Where(s => (s.Film.Title != null)) is not supported.

I wonder if there is a limitation on applying a filter on 3rd level in WCF Data services or it's a misconception on my side.


Solution

  • Seems that this guy here is right by using the ? (iif) operator like this "title= (s.Film == null) ? null : s.Film.Title" works fine. So the following expression will work fine even when the Film object is null.

    var _divsess = from d in Divisions select new 
      {d , sessionsfilms = from s in d.Sessions 
       select new {s, title= (s.Film == null) ? null : s.Film.Title }};