Category Archives: c#

LINQ to Entities Date Calculations

If you need to perform a DateDiff between 2 dates in your LINQ query:

var results = from r in db.Results
where r.Extra2 == id && r.CampaignID == 64
select new
{
Phone = r.Phone,
Pickup = r.PickUp,
Duration = EntityFunctions.DiffSeconds(r.PickUp,r.HangUp),
AnswerType = r.AnswerType

};

Notice the line - EntityFunctions.DiffSeconds(r.PickUp,r.HangUp)

C# Mapper Code

  1. Mapping ViewModel with Model.
  2. Make sure your viewmodel fields are named just like fields in model.
  3. EditMerchants (ViewModel)
  4. TsMerchants (Model)
  5. Downloaded via NuGet  - http://automapper.codeplex.com/
Mapper.CreateMap();
TsMerchant e = Mapper.Map(model);