Reputation: 412
I have this code
var list = _db.Projects.Where(item => item.Loc =="IN").Select(p => new {id=p.Id, title=p.Title,pc=p.PostalCode });
Project table having lot of columns, i need to query required columns dynamically and load from database, not all columns along with data.
Questions:
Upvotes: 3
Views: 8883
Reputation: 171236
Look at the expression the C# compiler generated and try to replicate what it does:
Expression<Func<Project, object>> lambda =
(Project p) => (object)new {id=p.Id, title=p.Title,pc=p.PostalCode };
I hope this code compiles. If not, you'll surely be able to fix it. Afterwards, look at the contents of the lambda
variable.
Note, that the cast to object
is only there to make this compile. You don't need/want that is production.
Upvotes: 1