我在oracle 11g中有一个表格如下:
Persons Table:
|ID |FirstName|LastName|PersianBirthDate|
|1 |FName1 |LName1 |"1362/01/01" |
|2 |FName2 |LName2 |"1359/05/01" |
|3 |FName3 |LName3 |"1350/04/11" |
|4 |FName4 |LName4 |"1355/02/10" |
|5 |FName5 |LName1 |"1365/12/25" |
当我想获得PersianDateBirthDate
小于"1362/01/01"
的行时,如下所示:
|ID |FirstName|LastName|PersianBirthDate|
|2 |FName2 |LName2 |"1359/05/01" |
|3 |FName3 |LName3 |"1350/04/11" |
|4 |FName4 |LName4 |"1355/02/10" |
我在EntityFramework中使用以下代码(代码优先):
using (var db = new MyContext())
{
return (db.Persons
.Where(x=>
PersianDateTime.Parse(x.PersianBirthDate).ToDateTime()
< PersianDateTime.Parse("1362/01/01").ToDateTime())
.ToList();
}
但发生以下异常:
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime()' method, and this method cannot be translated into a store expression.
我无法使用db.persons.AsEnumerable()
。
注意: PersianBirthDate 的数据类型为char(10)
,
PersianDateTime
库将波斯日期转换为格里高利日期。
答案 0 :(得分:1)
根据@Kristofer的评论,我们可以使用以下代码:
db.Persons
.Where(x => x.PersianBirthDate.CompareTo("1362/01/01") < 0).ToList();
然后将以下SQL发送到DB:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[PersianBirthDate] AS [PersianBirthDate]
FROM [dbo].[Persons] AS [Extent1]
WHERE [Extent1].[PersianBirthDate] < N'1362/01/01'
但我认为,我们可以改进这个解决方案。
答案 1 :(得分:-1)
这应该有效:
p.Where(x => ((int)x.PersianBirthDate.Substring(0,4)) < year) ||
(((int)x.PersianBirthDate.Substring(0,4)) = year) && ((int)x.PersianBirthDate.Substring(5,2) < month)) ||
(((int)x.PersianBirthDate.Substring(0,4)) = year) && ((int)x.PersianBirthDate.Substring(5,2) = month) && ((int)x.PersianBirthDate.Substring(8,2) <= day)