【发布时间】:2013-07-10 21:27:39
【问题描述】:
我写了一个自定义方法,如下所示:
DateTime GetGregorianDate(string date)
{
return new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(5, 2)), int.Parse(date.Substring(8, 2)), new System.Globalization.PersianCalendar());
}
我想在 linq 查询中调用这个方法,如下所示:
dynamic GetPlayerListByTeamName(string teamCode, FutsalEntities myEntity)
{
var player = (from p in myEntity.Players
where p.TeamCode == teamCode && (GetGregorianDate(p.ContractDateTo) <= DateTime.Now) ? true : false
select new { p.MeliCode, p.BearthDate, p.ContractNumber, p.InsuranceNumber, p.ContractDate, p.Mobile });
return player;
}
这是调用代码的地方:
private void cmbTeams_SelectedIndexChanged(object sender, EventArgs e)
{
using (FutsalEntities myEntity = new FutsalEntities())
{
if (cmbTeams.SelectedIndex != -1 && myEntity.Players.Count() != 0)
{
dgPlayerList.DataSource = GetPlayerListByTeamName(cmbTeams.SelectedValue.ToString(), myEntity);
但是当我运行应用程序时出现此错误:
LINQ to Entities 无法识别方法 'System.DateTime GetGregorianDate(System.String)' 方法,并且该方法无法转换为存储表达式。
有没有办法在 linq 查询中调用这个方法?
【问题讨论】:
-
几个提示——如果你有布尔运算,你不需要返回
true或false——只返回运算结果。我认为玩家应该有 Birth 日期。并尝试将日期存储为DateTime而不是字符串。 -
我将日期存储为字符串,因为 sqlsever 不支持 PresianDateTime,我需要将 PresianDateTime 转换为公历进行比较。我想我应该保存他们两个;我数据库中的波斯和公历日期。
标签: c# linq entity-framework