【发布时间】:2021-01-22 11:02:33
【问题描述】:
我有这个错误:
LINQ to Entities 无法识别方法:
GetDebitOuCredit
当我运行这个时:
using (SXEntities db = new SXEntities())
{
result = from ecriture in db.Ecrits
join compte in db.Comptes on ecriture.Compte equals compte.Compte1
join societe in db.Societes on ecriture.Societe equals societe.Societe1
select new EcritDTO
{
LibelleElement = compte.Libelle,
LienGED = string.Empty, // WIP
Compte = ecriture.Compte,
Collectif = ecriture.RacColtif,
Date = ecriture.DateC,
Journal = ecriture.Journal,
Piece = ecriture.Piece,
CodeOperation = ecriture.CodeOpe,
Libelle = ecriture.Libelle,
ReferenceDocument = ecriture.DocuRef,
// Error just below
Debit = GetDebitOuCredit("Debit", societe.DCMoins, ecriture.MtDeb, ecriture.MtCre),
};
}
我不能使用这个功能:
public double GetDebitOuCredit(string choix, string dcMoins, double? montantDebit, double? montantCredit)
{
double debit = 0;
double credit = 0;
bool isMontantNegatif = string.IsNullOrEmpty(dcMoins) && dcMoins == "1";
if(montantDebit != null && montantDebit != 0 && montantCredit != null && montantCredit != 0)
{
double difference = (double)montantDebit - (double)montantCredit;
if(difference < 0 && isMontantNegatif)
{
debit = 0;
credit = Math.Abs(difference);
}
else
{
debit = difference;
credit = 0;
}
}
return choix == "Debit" ? debit : credit;
}
我的问题:如何将我的函数 GetDebitOrCredit 转换为 LINQ 表达式?
【问题讨论】:
-
返回了多少行?
-
EF LINQ 查询被转换为 SQL。如何将本地函数转换为 SQL?
-
无论如何,该函数不需要是 LINQ 表达式。它可以很容易地成为
EcritDTO的计算属性,或者在加载对象 之后应用于对象。
标签: c# entity-framework linq expression