linq查询中不允许使用.date属性

时间:2010-12-10 20:49:27

标签: c# entity-framework

我正在使用我的linq查询,如下所示

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
             };

在上面的查询中,它抛出异常“LINQ to Entities不支持指定的类型成员'Date'。仅支持初始值设定项,实体成员和实体导航属性。” at .Date prperty

2 个答案:

答案 0 :(得分:1)

您需要确保仅比较两侧的日期部分。

测试用例:

  1. 如果您在leftSide中使用DateTime类型,则右侧也应该是DateTime类型,但不是Date类型。
  2. 同时检查您是否使用任何一方作为可以为空的值。
  3. 如果是DateTime类型,需要检查两侧的时间值。 例如,创建一个没有时间的日期时间并检查: - DateTime myDate = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day); var e =(来自myEntities.Table中的mds,其中mds.CreateDateTime> = myDate select mds).FirstOrDefault();
  4. 4.如果是日期类型,请使用日期类型进行检查。 例如,

    var query =来自db.MyTable中的e,其中e.AsOfDate< = DateTime.Now.Date选择e;

    如果你尝试过,你将能够接近解决方案。

    您可能需要在代码中执行以下操作:

    CreatedDate.Date = m.Create_Date.Date
    

答案 1 :(得分:1)

您可以使用SqlFunctions.DatePart提取要比较的DateTime的相关部分。见http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.datepart.aspx

相关问题