Search code examples
linqentity-frameworkentity-framework-4linq-to-entities

Anonymous Type with Lambda Expressions


I'm using EF, I have this query, I need to omit some information from the dataset. I was thinking to use anonymous type so I can have control on the data emitted.

Could you help me out to rewrite this query adding only the filed x.EventTitle and x.EventDateStart?

db.EventCustoms
  .Where(x => x.DataTimeStart > dateTimeNow & x.DataTimeStart <= dateTimeFuture);

Solution

  • That's what Select is for:

    db.EventCustoms
      .Where(x => x.DataTimeStart > dateTimeNow & x.DataTimeStart <= dateTimeFuture)
      .Select(x => new { x.EventTitle, x.EventDateStart, x.EventLocation });