sunny
sunny

Reputation: 355

.date property is not allowed in linq query

I am using my linq query as below

var result = from m in genDB.Membership_Association
             join ag in genDB.Account_Group on m.Account_Group_ID equals ag.Account_Group_ID
             join s in genDB.Account_Section on m.Account_Section_ID equals s.Account_Section_ID
             join p in genDB.Account_Package on m.Account_Package_ID equals p.Account_Package_ID
             join pinfo in genDB.ProductInfo on m.Product_ID equals pinfo.Product_Id
             where m.Control_Plan_ID == controlPlanId && m.Account_ID == accId
             select new MembershipViewModel
             {
                 GroupNumber = ag.Account_Group_Number,
                 SectionNumber = s.Account_Section_Number,
                 PackageNumber = p.Account_Package_Number,
                 ProductName = pinfo.Product_Name,
                 CreatedDate = m.Create_Date.Date,
                 EffectiveDate = m.Effective_Date.Value.Date,
                 CancelledDate = m.Cancel_Date.Value.Date,
                 Status = m.Status
             };

In the above query, it is throwing an exception "The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported." at .Date prperty

Upvotes: 1

Views: 361

Answers (2)

Tamilmaran
Tamilmaran

Reputation: 1347

You need to make sure that you are comparing only the Date part both side.

Test cases:

  1. If you use DateTime type in leftSide the rightside should also be in DateTime type but not Date type.
  2. Also check whether you have used any side as nullable value.
  3. If it is DateTime type, need to check time values both side. e.g., create a datetime with no time and check:- DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var e = (from mds in myEntities.Table where mds.CreateDateTime >= myDate select mds).FirstOrDefault();

4.If it is date type, check it with date type. For example,

var query = from e in db.MyTable where e.AsOfDate <= DateTime.Now.Date select e;

Atleast you will be able to go near the solution if you tried it.

Most probably you might have to do like this in your code:

CreatedDate.Date = m.Create_Date.Date

Upvotes: 1

Ian Mercer
Ian Mercer

Reputation: 39297

You can use SqlFunctions.DatePart to extract the relevant parts of the DateTime that you want to compare. See http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.datepart.aspx

Upvotes: 0

Related Questions