score:11

Accepted answer

Your issue is not linq. Your issue is that EF does not know how to convert that statement (z.JoiningDate.AddMonths(x.Commitments.Value)) to SQL.

If you ToList() before the select it should work.

Else you should use

EntityFunctions.AddMonths(z.JoiningDate, x.Commitments.Value)

which EF can indeed convert to SQL

In other words

.Select(z => new BondCompletionViewModel
{
   EmployeeId = z.EmployeeId.Value,
   EmployeeName = z.Name,
   StartDate = z.JoiningDate,
   EndDate =  EntityFunctions.AddMonths(z.JoiningDate, x.Commitments.Value)
}).ToList();

Edit: DbFunctions is the new EF 6 class to use instead of EntityFunctions

score:4

You can use EntityFunctions.AddMonths

This function is translated to a corresponding function in the database.

Usage

EntityFunctions.AddMonths(z.JoiningDate, x.Commitments.Value)

score:1

You can use SqlFunction Class:

.Where(x => SqlFunctions.DateAdd("month",x.Commitments.Value,x.JoiningDate).Month == ToDate.Value.Month)

Related Articles