using (dysoft.Data.CAccessDataClass oData = new dysoft.Data.CAccessDataClass()) { using (HW_MangerDataEntities ctx = new HW_MangerDataEntities(IoriBaseDataAccess.getEntityConnByIDBConnection(oData.sqlConn, "CustomerPlatform"))) { var linq = from row in ctx.tHWFinances where row.iState == 2 && row.iType == 1//申核通过的, 客户预付款 select row; if (string.IsNullOrEmpty(condition.clientCode) == false) { linq = linq.Where(row => row.cClientCode == condition.clientCode); } if (string.IsNullOrEmpty(condition.memo) == false) { linq = linq.Where(row => row.cMemo.Contains(condition.memo)); } if (condition.prePayEnum > 0) { linq = linq.Where(row => row.iType == (int)condition.prePayEnum); }
if (condition.dStart != null)
{
//linq = linq.Where(row => row.dDate >= condition.dStart);
//linq = linq.Where(row => System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(row.dDate, (DateTime)condition.dStart) >= 0);
//linq = linq.Where(row => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("d",row.dDate, (DateTime)condition.dStart) >= 0);
linq = linq.Where(row => System.Data.Objects.EntityFunctions.DiffDays((DateTime)condition.dStart, row.dDate) >= 0);
}
if (condition.dEnd != null)
{
//linq = linq.Where(row => DateTime.Compare(row.dDate, (DateTime)condition.dEnd) <= 0);
linq = linq.Where(row => System.Data.Objects.SqlClient.SqlFunctions.DateDiff("d", (DateTime)condition.dEnd, row.dDate) <= 0);
}
result = linq.ToList(); } }
exec sp_executesql N'SELECT [Extent1].[iID] AS [iID], [Extent1].[cSupplierCode] AS [cSupplierCode], [Extent1].[cClientCode] AS [cClientCode], [Extent1].[cDate] AS [cDate], [Extent1].[dDate] AS [dDate], [Extent1].[iState] AS [iState], [Extent1].[iType] AS [iType], [Extent1].[cMemo] AS [cMemo], [Extent1].[cProject] AS [cProject], [Extent1].[nMoney] AS [nMoney], [Extent1].[cAuditor] AS [cAuditor], [Extent1].[dAuditor] AS [dAuditor], [Extent1].[iFrom] AS [iFrom], [Extent1].[cCardID] AS [cCardID], [Extent1].[dCreate] AS [dCreate], [Extent1].[cOperator] AS [cOperator], [Extent1].[cCoCode] AS [cCoCode] FROM [dbo].[tHWFinance] AS [Extent1] WHERE (2 = CAST( [Extent1].[iState] AS int)) AND (1 = [Extent1].[iType]) AND ([Extent1].[cClientCode] = @p__linq__0) AND ( CAST( [Extent1].[dDate] AS datetime2) >= @p__linq__1) AND ([Extent1].[dDate] <= @p__linq__2)', N'@p__linq__0 varchar(8000), @p__linq__1 datetime2(7), @p__linq__2 datetime2(7)', @p__linq__0='01', @p__linq__1='2017-07-05 00:00:00', @p__linq__2='2017-07-29 00:00:00'
I am having two date values, one already stored in the database and the other selected by the user using DatePicker. The use case is to search for a particular date from the database. The value previously entered in the database always has time component of 12:00:00, where as the date entered from picker has different time component. I am interested in only the date components and would like to ignore the time component. What are the ways to do this comparison in C#? Also, how to do this in LINQ? UPDATE: On LINQ to Entities, the following works fine.
|
|||||
|
NOTE: at the time of writing this answer, the EF-relation was unclear (that was edited into the question after this was written). For correct approach with EF, check Mandeeps answer. You can use the
|
|||||||||||||||||||||
|
![](https://engine.adzerk.net/i.gif?e=eyJhdiI6NDM3LCJhdCI6NCwiYnQiOjAsImNtIjo2MDczNzEsImNoIjoxMTc4LCJjayI6e30sImNyIjozNTM3MDk5LCJkaSI6IjdmNTBjZGM4YzVmNTRmZDU4NTQyNGFhMDZmYzMwNjRjIiwiZG0iOjEsImZjIjo2MTExNDAwLCJmbCI6NjIxMDIyOCwiaXAiOiI2NS40OS42OC4xODQiLCJrdyI6ImMjLGxpbnEsZW50aXR5LWZyYW1ld29yayxsaW5xLXRvLWVudGl0aWVzLGRhdGV0aW1lLWNvbXBhcmlzb24iLCJudyI6MjIsInBjIjoxLjY1LCJlYyI6MS42NSwicHIiOjk1NjA0LCJydCI6MiwicmYiOiJodHRwOi8vd3d3LmJhaWR1LmNvbS9zP2llPXV0Zi04JmY9OCZyc3ZfYnA9MSZ0bj1iYWlkdSZ3ZD1saW5xIFRPIEVOVElUWSAsZGF0ZXRpbWUgQ29tcGFyZVRvLCBzaXRlOnN0YWNrb3ZlcmZsb3cuY29tJm9xPWxpbnElMjAlMkNkYXRldGltZSUyMCUyNmx0JTNCb21wYXJlVG8lMkMlMjBzaXRlJTNBc3RhY2tvdmVyZmxvdy5jb20mcnN2X3BxPWFhYTk0NDQyMDAwMTUxZTMmcnN2X3Q9YWMxZXBoOEE5eTNDME4xSy9tQThEckE0UkNZNVozeVlDT2RtMXl3WXlHV09CRUtxcFdDNXBxSk1wRDAmcnFsYW5nPWNuJnJzdl9lbnRlcj0xJmlucHV0VD00OTIzNiZyc3Zfc3VnMz04OSZyc3Zfc3VnMj0wJnJzdl9zdWc0PTUwNzE0Iiwic2EiOiIzIiwic2IiOiJpLTBkMGIwZmIxZDNiYTMwMDlhIiwic3AiOjY4NDAsInN0Ijo4Mjc3LCJ1ayI6InVlMS1lMWRlMDAyYTdmYmU0MTQ0YTk3ZjY0MWMzYzcyMGJkYiIsInpuIjo0NCwidHMiOjE1MDExMzg1NTY0NjgsImJmIjp0cnVlLCJwbiI6ImFkemVyazEzMTEyNTE3MjYiLCJiYSI6MSwiZnEiOjB9&s=U92av80lMmHlnXIeuUr1ZDOroHY)
Use the class
UPDATE As of EF 6.0 and later EntityFunctions is replaced by DbFunctions. |
|||||||||||||
|
I think this could help you. I made an extension since I have to compare dates in repositories filled with EF data and so .Date was not an option since it is not implemented in LinqToEntities translation. Here is the code:
then you can use it in this way.
|
||||
If you use the
You can use something like this:
|
|||
To do it in LINQ to Entities, you have to use supported methods:
Ugly, but it works, and it's done on the DB server. |
|||
Here's a different way to do it, but it's only useful if SecondDate is a variable you're passing in:
I think that should work |
|||||||||||||||||
|
You can also use this:
|
|||
Just always compare the Date property of DateTime, instead of the full date time. When you make your LINQ query, use date.Date in the query, ie:
|
|||||||||||||||||
|
Just if somebody arrives here googling or binging... compiled workarounds: |
|||
This is how I do this.
|
|||
//Note for Linq Users/Coders This should give you the exact comparison for checking if a date falls within range when working with input from a user - date picker for example:
where startdate and enddate are values from a date picker. |
||||
You can user below link to compare 2 dates without time :
the Compare function return 3 different values: -1 0 1 which means dt1>dt2, dt1=dt2, dt1 |
|||||
|
you can use DbFunctions.TruncateTime() method for this.
|
|||
Without time than try like this:
|
||||
Try this... It works fine to compare Date properties between two DateTimes type: PS. It is a stopgap solution and a really bad practice, should never be used when you know that the database can bring thousands of records...
|
|||||||||||||||||||||
|
南来地,北往的,上班的,下岗的,走过路过不要错过!
======================个性签名=====================
之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗?
下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!!
如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.