Linq中的datepart函数 [英] datepart function in Linq

查看:170
本文介绍了Linq中的datepart函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表DOB,

其中有两列Name和dateOfBirth,

现在我想要计算总行数(结果)

1. jan月,2012年,

2. feb月,2012年,

等,



所以我如何使用where条件,在同一列名称dateOfBirh,

这是在日期时间formet,

如12 / 7/2012 12:24:00:PM,







Plz在Linq提供解决方案,

因为我在Sql中找到了这样的解决方案,





I have a table "DOB",
Which have two columns "Name" and "dateOfBirth",
now i want calculate total rows(result) which have
1. jan month,2012 year,
2. feb month,2012 year,
etc,

so how i use where condition, in the same column name "dateOfBirh" ,
which is in date time formet,
like 12/7/2012 12:24:00:PM,



Plz provide solution In Linq,
because i found solution in Sql like this,


select * from TableName

where datepart(mm, DateColumnName) = 1

and datepart(yyyy, DateColumnName) = 2012

推荐答案

我假设你想要t检索出生日期是2012年1月或2012年2月的所有记录。如果是这种情况,你可以使用 DATEPART [ ^ ]过滤你的行。

尝试

I am assuming that you want to retrieve all the records where date of birth is on Jan 2012 or Feb 2012. If that''s the case you can use DATEPART[^] to filter your rows.
try
SELECT COUNT(*) AS TotalRows FROM DOB
WHERE (DATEPART(Month,dateOfBirth) = 1 OR DATEPART(Month,dateOfBirth) = 2) AND (DATEPART(year,dateOfBirth) = 2012)





编辑:

SqlFunctions类 [ ^ ]可能会对您有所帮助。请查看 SqlFunctions.DatePart方法 [ ^ ]。

另请查看此CP文章 LINQ查询仅比较DateTime的日期部分 [< a href =http://www.codeproject.com/Articles/499987/LINQ-query-to-compare-only-date-part-of-DateTimetarget =_ blanktitle =New Window> ^ ]




SqlFunctions Class[^] might help you. Take a look at SqlFunctions.DatePart Method[^].
Also check out this CP article LINQ query to compare only date part of DateTime[^]


解决方案是:

solution is:
var data1 = context.t_quoted_value.Where(x => x.region_name == "Hong Kong"
                            && x.price_date.Value.Year == dt.Year
                            && x.price_date.Value.Month == dt.Month
                            && x.price_date.Value.Day == dt.Day).ToList();


这篇关于Linq中的datepart函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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