Linq to SQL datetime抓取问题 [英] Linq to SQL datetime fetching problem
问题描述
在我的旧版本项目中,我必须使用以下SQL查询从SQL检索记录
转换(日期,< DATETIME VARIABLE>,101)和转换(转换后的日期,< DBCOLUMNNAME>,101) date,< DATETIME VARIABLE>,101)
以上SQL查询提取记录我们的预期,
例如
其中Convert(date,FollowupDate,101)之间转换(日期,''12/10/2012 12:00:00 AM'',101)和转换(日期,''12/10/2012 12:00:00 AM'',101)
结果将是,
记录FollowupDate =''12 / 10/2012''(MM / dd / yyyy)
现在我们正在使用linq,
这里将相同的SQL转换为
Quote:LeadDataContext ds = new LeadDataContext();
DateTime fd = DateTime.ParseExact(12/10/20 12,MM / dd / yyyy,null);
var g =来自a的ds.Leads
其中a。 FollowupDate> = fd.Date&& a.FollowupDate< = fd.Date
选择一个;
但是它我在特定日期搜索时没有检索记录(12/10/2012)(两个都是相同的日期)
我在我的数据库中找到了FollowupDate存储as''
2012-12-10 10:00:00.000
但是我需要检查没有时间戳因此我需要在没有时间戳的情况下转换''Followupdate''列,就像我在上面的SQL查询中所做的那样。
即使我用变量fd检查它检索12 / 10/2012 12:00:00 AM因此我试图将fd转换为12/10/2012 00:00:00,但它不起作用..
请帮帮我..我是LINQ的新手,如果我在语法中犯了一些错误,请原谅我
我自己找到了解决方案,
我在这里为我的三年级发布了它..
i应该使用语法linq为
其中a.FollowupDate.Value.Date> = fd.Date&& a.FollowupDate.Value.Date< = fd.Date
双方平等拥有相同的属性(日期),因此它比较''12 / 10/2012 12:00:00 AM''> =''12/10/2012 12:00:00 AM''因此它检索。
Hi,
In my old version of my project i have to used to retrieve records from SQL using the following SQL Query
where Convert(date,<DBCOLUMNNAME>,101) between Convert(date,<DATETIME VARIABLE>,101) and Convert(date,<DATETIME VARIABLE>,101)
Above SQL Query fetch records what we expect,
For example
where Convert(date,FollowupDate,101) between Convert(date,''12/10/2012 12:00:00 AM'',101) and Convert(date,''12/10/2012 12:00:00 AM'',101)
The result will be,
Records with FollowupDate = ''12/10/2012'' (MM/dd/yyyy)
Now we are using linq ,
Here the same SQL is converted as
Quote:LeadDataContext ds = new LeadDataContext();
DateTime fd = DateTime.ParseExact("12/10/2012","MM/dd/yyyy",null);
var g = from a in ds.Leads
where a.FollowupDate >= fd.Date && a.FollowupDate <= fd.Date
select a;
But Its not retrieving the record while i search on particular date(12/10/2012)(both are same dates)
I have found in my DB, the FollowupDate is stored as ''
2012-12-10 10:00:00.000
but i need to check without timestamp hence i need to convert ''Followupdate'' column without timestamp as like i did in above SQL query.
Even i checked with variable "fd" it retrieves "12/10/2012 12:00:00 AM" hence i tried to convert "fd" as "12/10/2012 00:00:00" but its not works..
Please help me out.. I''m newer to LINQ forgive me if i made some mistake in Syntax
I found the solution myself,
I posted it here for my Juniors..
i should use syntax of linq as
where a.FollowupDate.Value.Date >= fd.Date && a.FollowupDate.Value.Date <= fd.Date
Both sides of equal are having same Property here(Date), hence it compares ''12/10/2012 12:00:00 AM'' >= ''12/10/2012 12:00:00 AM'' hence it retrieves.
这篇关于Linq to SQL datetime抓取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!