在LINQ查询中无时间返回日期 [英] Returning dates without times in a LINQ query
问题描述
我正在编写一个查询,我想计算我们的呼叫中心按日期联系的次数。看起来很简单,但是由于联系人日期字段是一个datetime字段,所以我得到时间,所以当我按联系日期(时间)分组时,每个联系人日期实例都有一个'1'。所以,我只想按时间分组。以下是我用来查询数据库的表达式:
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()
})。(50);
我试图使用
h.contact_dt.ToShortDateString()
但这不工作,因为SQL确实知道ToShortDateString()。非常感谢任何想法。
感谢!
编辑:
.Take(50)是为了限制我得到的记录数,直到我添加日期选择器和where子句,所以我没有得到所有的400K记录。 ;)
使用 Date
property:
group h by h.contact_dt.Date into g
LINQ to SQL知道这一点 - 请参阅这个方便的页面的理解方法和属性。
编辑:好的,所以直接分组不起作用...我们试图给它一点的帮助:
var items =(from h in db.ContactHistories
select h.Date into date
组日期到g
orderby g.Key
选择新
{
contact_dt = g.Key,
item_count = g.Count()$ b $ (50);
我不知道是否可以正常工作,但值得一试...
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()
But that doesn't work since SQL does know about ToShortDateString(). Any thoughts are greatly appreciated.
Thanks!
EDIT:
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. ;)
Use the Date
property:
group h by h.contact_dt.Date into g
LINQ to SQL knows about that - see this handy page of "understood" methods and properties.
EDIT: 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屋!