【问题标题】:.date property is not allowed in linq query.date 属性在 linq 查询中是不允许的
【发布时间】:2010-12-10 20:49:27
【问题描述】:

我正在使用我的 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 属性

【问题讨论】:

  • 是的。这是不允许的。 (有什么问题?)
  • 谢谢大卫。有没有其他方法可以获取日期
  • 我只需要获取指定日期时间字段的日期,我需要在 linq 查询中获取它

标签: c# entity-framework


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您需要确保只比较双方的日期部分。

    测试用例:

    1. 如果你在 leftSide 中使用 DateTime 类型,那么右侧也应该是 DateTime 类型,而不是 Date 类型。
    2. 还要检查您是否使用任何一侧作为可空值。
    3. 如果是DateTime类型,需要检查双方的时间值。 例如,创建一个没有时间的日期时间并检查:- 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.如果是日期类型,请检查日期类型。 例如,

    var query = from e in db.MyTable where e.AsOfDate

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

    您很可能必须在代码中这样做:

    CreatedDate.Date = m.Create_Date.Date
    

    【讨论】:

      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 2020-12-30
      相关资源
      最近更新 更多