Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ to实体中不支持“日期”。仅支持初始值设定项、实体成员和实体导航属性_C#_Entity Framework_Linq_Linq To Entities - Fatal编程技术网

C# LINQ to实体中不支持“日期”。仅支持初始值设定项、实体成员和实体导航属性

C# LINQ to实体中不支持“日期”。仅支持初始值设定项、实体成员和实体导航属性,c#,entity-framework,linq,linq-to-entities,C#,Entity Framework,Linq,Linq To Entities,我试图执行以下代码,但收到一个错误 public List<Log> GetLoggingData(DateTime LogDate, string title) { var context = new LoggingEntities(); var query = from t in context.Logs where t.Title == title && t.Timestamp == LogDa

我试图执行以下代码,但收到一个错误

public List<Log> GetLoggingData(DateTime LogDate, string title)
{
     var context = new LoggingEntities();
     var query = from t in context.Logs

           where t.Title == title 
           && t.Timestamp == LogDate

           select t;
     return query.ToList();
}

我收到的错误是LINQ to实体中不支持指定的类型成员“Date”。仅支持初始值设定项、实体成员和实体导航属性。我尝试了各种尝试,将每个符号都转换为字符串,只比较日期部分,但似乎无法得到正确的组合。非常感谢您的帮助。

这不是最好的解决方案,但它很有效。出于各种原因,我现在不得不使用.NET3.5,修改数据库将很困难。无论如何,这里有一个有效的解决方案:

            var query = from t in context.Logs
                      where t.Title == title 
                      && t.Timestamp.Day == LogDate.Day
                      && t.Timestamp.Month == LogDate.Month
                      && t.Timestamp.Year == LogDate.Year
                      select t;

不是最优雅的解决方案,但它是有效的。

如果我错了,请纠正我,但在mikemurf22的示例中,它需要检查日期组件的每个部分,并且可能需要更多的服务器处理

无论如何,我偶然发现了这个问题,这就是我的解决方案

假设只传入日期组件,则可以找到传入的当天的最后一分钟,并使用where子句定义范围

public List<Log> GetLoggingData(DateTime LogDate, string title)
{
    DateTime enddate = new DateTime(LogDate.Year, LogDate.Month, LogDate.Day, 23, 59, 59)

    var query = from t in context.Logs
                where t.Timestamp >= date
                where t.Timestamp <= enddate
                select t;

    return query.ToList();
}
您可以使用此黑客:

DateTime startDate = LogDate.Date;
DateTime endDate = LogDate.Date.AddDays(1);

var query = from t in context.Logs
            where t.Title == title 
                  && t.Timestamp >= startDate 
                  && t.Timestamp < endDate
            select t;

如果您使用的是EF 6.0+,则可以使用:

注意:对于早期版本的EF,DbFunctions不可用,可以改用


始终对x.DateTimeStart和LogDate使用EntityFunctions.TruncateTime。 例如:

var query = from t in context.Logs
              where t.Title == title 
              && EntityFunctions.TruncateTime(t.Timestamp) == EntityFunctions.TruncateTime(LogDate)
              select t;
将LongDate转换为.ToShortDateStrings,然后您可以这样使用它:

EntityFunctions.TruncateTime(t.Timestamp) == LogDate
就像mike做的那样尝试一下:

var calDate = DateTime.Now.Date.AddDays(-90);

var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate)
已从EF6中过时

下面使用


向我们展示Logs类及其映射到的表。我正在将EFv1与.NET3.5一起使用。log类只是企业库中的一个日志表,标题为字符串类型,时间戳为datetime字段。我只想比较一下DatePart你还需要截断你的LogDate.Dateproperty@invalidusernameLogDate是一个日期时间,这意味着LogDate.Date被截断为+1。在我的案例中,我无法使用公认的答案,因为我做了一个“大于”的比较,在这个比较中,你不能分别在日、月和年进行比较。我不知道函数。顺便说一句,该函数现在已过时,并已移动到另一个程序集。改为使用“DbFunctions.TruncateTimet.Timestamp”。我稍微更正了一下。当然,您不能在查询中使用AddDays方法。但是你可以把它移到外面。因此,变通方法仍然有效。LINQ to实体中不支持指定的类型成员“Date”。仅支持初始值设定项、实体成员和实体导航属性。
var calDate = DateTime.Now.Date.AddDays(-90);

var result = return (from r in xyz where DbFunctions.TruncateTime(r.savedDate) >= DbFunctions.TruncateTime(calDate)