LINQ查询不同的日期 [英] linq query distinct date

查看:71
本文介绍了LINQ查询不同的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的查询中,如何删除datetime列的de time部分以仅获取不同的日期?

On the query below how can remove de time part of the datetime column to get only the distinct dates?

using (var ctx = new DBConn())
{
  var q = ctx.Table.Select(r => r.datetimefield).Distinct();
}

推荐答案

您正在为linq-to-sql或EF(我不知道您使用的是哪个)而遇到了麻烦.直接的解决方案是使用DateTime.Date属性,但是,两个ORM均不支持此属性,它不映射到SQL方法.

You are stumbling over a fault with linq-to-sql or EF (I don't know which one you are using). The straight forward solution would be to use the DateTime.Date property, however, this is unsupported in both ORMs, it doesn't map to an SQL method.

您有两种解决方案:

  1. 通过使用ToList来快速评估查询:

var q = ctx.Table.ToList().Select(r => r.datetimefield.Date).Distinct();

  • 创建一个仅详细描述数据库中日期组件的字段.我通常会这样做,因为您可以留在数据库中并在其中执行查询.

  • create a field detailing only the date component in the database. This is the way I usually go as you can stay in the database and execute the query there.

    这篇关于LINQ查询不同的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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