Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

c# - Function in linq select

I have this error :

LINQ to Entities does not recognize the method: GetDebitOuCredit

When I run this:

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),
             };
}

I cannot use this function:

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;
}        

My question: How can I transform my function GetDebitOrCredit to LINQ expression?

question from:https://stackoverflow.com/questions/65843898/function-in-linq-select

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually if it is final projection, you don't have to translate this function. If not, you have to use third party extensions for that:

https://github.com/hazzik/DelegateDecompiler

https://github.com/axelheer/nein-linq/

Never tried DelegateDecompiler, so will show sample using NeinLinq

[InjectLambda]
public double GetDebitOuCredit(string choix, string dcMoins, double? montantDebit, double? montantCredit)
{
   _getDebitOuCreditFunc ??= GetDebitOuCredit().Compile();
   return _getDebitOuCreditFunc(choix, dcMoins, montantDebit, montantCredit);
}

static Func<string, string, double?, double?, double> _getDebitOuCreditFunc;

public static Expression<Func<string, string, double?, double?, double>> GetDebitOuCredit()
{
    return (choix, dcMoins, montantDebit, montantCredit) =>
       montantDebit != null && montantDebit != 0 && montantCredit != null && montantCredit != 0) 
       ? ((double)montantDebit < (double)montantCredit) && dcMoins == "1" 
          ? choix == "Debit" ? 0 : (double)montantCredit) - (double)montantDebit
          : choix == "Debit" ? (double)montantDebit - (double)montantCredit) : 0
        : 0;
}

Then you can use your function in LINQ queries after ToInjectable() call.

result = from ecriture in db.Ecrits.ToInjectable()
         join compte in db.Comptes on ecriture.Compte equals compte.Compte1
         ...
                             

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...