Linq query error

后端 未结 4 1581
渐次进展
渐次进展 2021-01-06 19:20

I am using following Linq query:

from p in People
 where p.Name == \"George Lucas\"
select p.TitlesActedIn

where TitlesActedIn is a list. P

4条回答
  •  梦谈多话
    2021-01-06 19:53

    UPDATED: See this question and answer to understand the limitations on Select Many in Data Services + Another solution based on $expand (note this requires the server to support expand)

    If this is WCF Data Services and TitlesActedIn is a collection of related movies. Then you can do this in one query only if Person.Name is the primary key.

    To illustrate this:

    var titles = from p in people
                 where p.Name == "George Lucas"
                 from m in p.TitlesActedIn
                 select m;
    

    Will do what you want but only if Name is the key of the Person entity, otherwise this is unsupported.

    If Name is not the key one way to do this (today) is with two queries, something like this:

    var key = (from p in people
              where p.Name == "George Lucas"
              select new {p.Id}).Single().Id;
    
    var titles = from p in people
                 where p.Id == key
                 from m in p.TitlesActedIn
                 select m;
    

    Another option though would be do an expand:

    var george = (from p in people.Expand("TitlesActedIn")
                 where p.Name == "George Lucas"
                 select p).Single();
    
    var titles = george.TitlesActedIn;
    

    But that relies on the server supporting $expand - which not all servers do...

    Note we are currently working on adding any/all support to OData and WCF Data Services, once that is released you would be able to write:

    var titles = from t in titles
                 where t.Actors.Any(a => a.Name == "George Lucas")
                 select t;
    

    Hope this helps

    Note: in the code that gets the key for George Lucas I create an anonymous type because today WCF Data Services doesn't support materializing primitives directly.

提交回复
热议问题