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:
- how to write lambda expressions for linq select ?
- How to reduce data reads on database by selecting specific cols, entity framework ?
Look at the expression the C# compiler generated and try to replicate what it does:
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.