LINQ到实体组的故障使用.date [英] LINQ to Entities group-by failure using .date

查看:129
本文介绍了LINQ到实体组的故障使用.date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过只是时间字段的日期部分做一个LINQ组。

I am trying to do a Linq group by on just the date part of a datetime field.

这LINQ声明的日期和时间的作品,但它组。

This linq statement works but it groups by the date and the time.

var myQuery = from p in dbContext.Trends
          group p by p.UpdateDateTime into g
          select new { k = g.Key, ud = g.Max(p => p.Amount) };

当我刚好我得到以下错误的日期运行此语句组

When I run this statement to group by just the date I get the following error

var myQuery = from p in dbContext.Trends
          group p by p.UpdateDateTime.Date into g   //Added .Date on this line
          select new { k = g.Key, ud = g.Max(p => p.Amount) };

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

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

我如何通过的日期,而不是​​日期和时间做一组?

How can I do a group by for the date and not the date and time?

推荐答案

使用 EntityFunctions.TruncateTime 方法:

var myQuery = from p in dbContext.Trends
          group p by EntityFunctions.TruncateTime(p.UpdateDateTime) into g
          select new { k = g.Key, ud = g.Max(p => p.Amount) };

这篇关于LINQ到实体组的故障使用.date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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