c# - Is it possible to return a struct from a Linq2Entities query? -
this code generates runtime error:
struct mystruct { public int id; public string desc; } ... var q = db.mytable.select(t => new mystruct { id = t.id, desc = t.desc }); the error happens because there's no parameterless constructor struct. can't use explicit constructor, because there's no sql translation that.
my first question why doesn't generate compile-time error - compiler knows mystruct struct , can't use object initializer on non-existent parameterless constructor.
but more significant question is, known limitation of linq2entities, have use classes rather structs?
1) compiler doesn't know linq entities does, , shouldn't.
2) yes, known.
a fix though:
var q = db.mytable //do processing here (where, any, join, whatever) .tolist() //or asenumerable or toarray .select(t => new mystruct { id = t.id, desc = t.desc });
Comments
Post a Comment