Search code examples
linqodatanetflix

How to properly use anonymous type with Netflix OData API


I am trying to use the query below in LINQPad. It isnt working. I am getting this exception:

NotSupportedException: Constructing or initializing instances of the type <>f__AnonymousType0`1[System.String] with the expression t.BoxArt.SmallUrl is not supported.

from t in Titles where t.Id == "ApUFq" select new { t.BoxArt.SmallUrl }

Solution

  • I'm not familiar with the Netflix OData API, but your issue appears to be a common stumbling block with LINQ.

    Try this instead:

    from t in Titles
    where t.Id == "ApUFq"
    select new t.BoxArt.SmallUrl;
    

    Or alternatively:

    from t in Titles.Where(t0 => t0.Id == "ApUFq").ToArray()
    select new { t.BoxArt.SmallUrl };
    

    One or both should work for you.