Reputation: 101
i write a custom method like below :
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());
}
and i want call this method in linq query like below :
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;
}
This is where the code is called :
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);
but when i run application get this error :
LINQ to Entities does not recognize the method 'System.DateTime GetGregorianDate(System.String)' method, and this method cannot be translated into a store expression.
Is there any way to call this method in linq query?
Upvotes: 4
Views: 2231
Reputation: 4573
Convert DateTime.Now to Persian Date, store it in a variable and then use that variable in the query, so that the server side query will do a persian date comparison.
Upvotes: 0
Reputation: 236188
Linq to Entities can't translate your method into SQL and execute it on server side. The only way to call your custom method is to move query into memory - e.g. by calling AsEnumerable()
or ToList()
var players = from p in myEntity.Players.AsEnumerable()
where GetGregorianDate(p.ContractDateTo) <= DateTime.Now
select new {
p.Name,
p.BearthDate,
p.ContractNumber,
p.InsuranceNumber,
p.ContractDate,
p.Mobile
};
Keep in mind, that this will transfer all players data over the network to local computer.
Upvotes: 4