合计LINQ查询实体框架对象限制 [英] Entity Framework object limitations in aggregate LINQ query

查看:128
本文介绍了合计LINQ查询实体框架对象限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想有返回一些特定类型的一个相当复杂的查询。天色关于日期/时间计算或字符串值,实体框架似乎随地吐痰的假人当我尝试比System.DateTime的任何类型进一步:

I have a rather complicated query that I'd like to have return some particular types. Mostly concerning date / time calculations or string values, the Entity Framework seems to be spitting the dummy when I try anything further than a System.DateTime type:

指定类型的成员'日期'是
  LINQ中不支持的实体。
  只有初始化,实体成员,
  实体导航属性
  支持。

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

有问题的查询是:

    // Here comes the aggregate query.
    object summary = from te in ctx.TimeEntries
                     where te.StartTime >= startofweek
                     && te.UserName == User.Identity.Name
                     group te by te.StartTime.Day into Groups
                     select new
                     {
                         Key = Groups.Key,
                         Count = Groups.Count(),
                         //Total = Groups.Sum(n => (double)((n.EndTime ?? DateTime.Now) - n.StartTime).TotalMinutes / 60),
                         Tasks = from t in Groups
                                 orderby t.StartTime descending
                                 select new
                                 {
                                     Name = t.Task.TaskName,
                                     Project = t.Task.Batch.Project.ProjectName,
                                     Batch = t.Task.Batch.BatchName,
                                     //Minutes = ((t.EndTime ?? DateTime.Now) - t.StartTime).Minutes,
                                     Start = t.StartTime.Date,
                                     Stop = t.EndTime,
                                     Description = t.Notes,
                                     Breaks = t.BreakFor ?? 0
                                 }
                     };

在上面的查询中,财产开始将失败与上述错误。不过,如果我只是有开始= t.StartTime一切都会好起来。同样,对纪要中注释掉值也会引起问题。

In the query above, the property "Start" will fail with the aforementioned error. However, if I simply have "Start = t.StartTime" everything will be fine. Similarly, the commented out value for "Minutes" will also cause problems.

建议最欢迎!

推荐答案

您只能使用支持成员L2E查询,的 日期是不是那些之一。

You can only use supported members in L2E queries, and Date isn't one of those.

一个解决方法是打破你的单L2E查询到一个L2E查询后跟一个LINQ到对象查询:

One workaround is to break your single L2E query into one L2E query followed by one LINQ to Objects query:

var q = from e in Context.Entities
        select new
        {
            Id = e.Id,
            DateTime = e.DateTime
        };

var r = from e in q.AsEnumerable()
        select new
        {
            Id = e.Id,
            Date = e.DateTime.Date
        };

这篇关于合计LINQ查询实体框架对象限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆