Asp.net 在linq查询中获取给定todate和fromdate中的记录
我有下面的CliientDetails表 **CliGenDate varchar50 Clinamevarchar 2013年7月26日a 2013年7月25日b 2013年7月24日c 2013年7月23日d 2013年7月22日e** 假设我的todateDateTime为2013年7月22日,fromDateDateTime为2013年7月24日 结果将是 **CliGenDate varchar50 Clinamevarchar 2013年7月24日c 2013年7月23日d 2013年7月22日e** 我想要Linq查询 我尝试了以下代码,但给出了一个错误消息Asp.net 在linq查询中获取给定todate和fromdate中的记录,asp.net,linq,entity-framework,linq-to-sql,entity-framework-5,Asp.net,Linq,Entity Framework,Linq To Sql,Entity Framework 5,我有下面的CliientDetails表 **CliGenDate varchar50 Clinamevarchar 2013年7月26日a 2013年7月25日b 2013年7月24日c 2013年7月23日d 2013年7月22日e** 假设我的todateDateTime为2013年7月22日,fromDateDateTime为2013年7月24日 结果将是 **CliGenDate varchar50 Clinamevarchar 2013年7月24日c 2013年7月23日d 2013
var Result =
(from a in Context.CliientDetails.AsEnumerable()
where a.cliUserName == SalesEmail
&& DateTime.Parse(a.cliGenDate)>=todate
&& DateTime.Parse(a.cliGenDate)>=FromDate
select new NewClientReg_BusinessLayer.Models.viewClientDetails
{
cliGenDate = a.cliGenDate,
cliAckNo = a.cliAckNo,
cliFullName = a.cliFullName,
cliTopic = a.cliTopic,
cliCourse = a.cliCourse,
cliDomain = a.cliDomain,
cliTotalInvest = a.cliTotalInvest
});
但它给出了以下错误消息
字符串未被识别为有效的DateTime。请尝试使用而不是DateTime.Parse
然后
好问题是:为什么要将日期存储为varchar
您可能知道,使用.AsEnumerable,您将在应用where子句之前检索所有数据
你至少可以这样做
from a in Context.CliientDetails.Where(c => c.cliUserName == SalesEmal).AsEnumerable()
...
&& DateTime.ParseExact(a.clieGenDate, "dd/MM/yyyy", provider) >= todate
from a in Context.CliientDetails.Where(c => c.cliUserName == SalesEmal).AsEnumerable()
...