How to use anonymous types with Netflix OData API
If I create this collection:
IEnumerable<_TITLE> _titles = null;
开发者_Python百科
How can I set the result set from Netflix in this expression:
_titles = from t in Titles where t.Id == "ApUFq" select new {t, t.Cast}
I am getting:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable<_Title>'. An explicit conversion exists (are you missing a cast?)
I understand it's because I am using an anonymous type. _TITLE is a custom complex object I create that exposes a subset of Netflix properties.
If I add "_TITLE" in front the of the "new" it says that my object does not implement IENUMBERABLE.
If then _TITLE implements IENUMBERABLE, it says _TITLE does not contain a definition for ADD and still getting conversion error.
Basically, I want to set the _TITLE properties with the data returned from Netflix. I have defined _TITLE to contain the fields I need, plus a collection of PERSON (for Cast).
If your generic type is _TITLE, then you need to do select new _TITLE {Prop = Value}
.
If you intend to use a generic type, then you need to use var
:
var _titles = from t in Titles where t.Id == "ApUFq" select new {t, t.Cast};
So perhaps this is what you meant:
var _titles = from t in Titles where t.Id == "ApUFq" select new _TITLE {Title = t};
Something along those lines. var
can be used even if you don't intend to use an anonymous type.
In addition to vcsjones's answer, You don't need to implement IEnumerable on _TITLE. What's going on is that when you write
var foo = new TYPENAME { a, b };
it means:
- create a new TYPENAME, which implements IEnumerable
- call foo.Add(a) then foo.Add(b);
Example:
var names = new List<string> { "Joe", "Mary", "Susan" };
As vcsjones mentioned, You need to use Parameter names when initializing an object, or perhaps you meant to use parentheses and call a constructor
var title = new _TITLE { Title = t.ID, Cast = t.Cast };
// or
var title = new _TITLE (t.ID, t.Cast);
精彩评论