在LINQ查询返回的日期没有时间 [英] Returning dates without times in a LINQ query

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

问题描述

我写一个查询,我想算的时候,我们的呼叫中心被按日期接触的次数。似乎很容易,但由于接触日期字段是一个日期时间字段我得到的时间,所以当我组通过接触日期(时间)每个联系人的日期实例有1的计数。所以,我想通过组不只是时间的日期。下面是我用它来查询数据库中的表达式:

I am writing a query where I want to count the number of times our call center gets contacted by date. Seems easy enough, but since the contact date field is a datetime field I get the time, so when I group by contact date(time) each contact date instance has a count of '1'. So, I want to group by just the date without the time. Below is the expression I use to query the database:

MyDataContext db = new MyDataContext();
var items = (from h in db.ContactHistories
             group h by h.contact_dt into g
             orderby g.Key
             select new
             {
                 contact_dt = g.Key,
                 item_count = g.Count()
             }).Take(50);



我试图用

I've tried to use

     h.contact_dt.ToShortDateString()

但是,这并不因为工作不SQL了解ToShortDateString()。 。任何想法是极大的赞赏。

But that doesn't work since SQL does know about ToShortDateString(). Any thoughts are greatly appreciated.

感谢

编辑:

。取(50)是否到位,以限制我得到的记录数,直到我添加了日期选择器和一个where子句,所以我没有得到所有的400K记录。 ;)

The .Take(50) is in place to limit the number of records I get until I add the date pickers and a where clause so I don't get all 400K records. ;)

推荐答案

使用日期属性:

group h by h.contact_dt.Date into g

的LINQ to SQL知道有关 - 见的这个方便的网页 理解的方法和属性。

LINQ to SQL knows about that - see this handy page of "understood" methods and properties.

编辑:好的,所以分组直接不起作用......怎么样,我们试图给它一点帮助:

Okay, so grouping directly doesn't work... how about we try to give it a bit of help:

var items = (from h in db.ContactHistories
             select h.Date into date
             group date by date into g
             orderby g.Key
             select new
             {
                 contact_dt = g.Key,
                 item_count = g.Count()
             }).Take(50);



我不知道它会工作,但它是值得一试...

I don't know if it'll work, but it's worth a try...

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

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