Comment placer un if else dans cette instruction linq?
where instruct.InstructorInstrNo == royalHIstory.RoyalIns.ToString()
Je voudrais faire quelque chose comme
where instruct.InstructorInstrNo == if(royalHIstory.RoyalIns.ToString().Length == 3) "0" + royalHIstory.RoyalIns.ToString() else royalHIstory.RoyalIns.ToString()
Je pense que quelque chose de similaire peut être fait lors de l'utilisation d'expressions lambda, mais ma requête ne le fait pas et je ne sais pas comment convertir ma requête pour n'utiliser que des expressions lambda
C'est ce qu'on appelle l' opérateur ternaire ou opérateur conditionnel ternaire (? :)
where instruct.InstructorInstrNo ==
(royalHIstory.RoyalIns.ToString().Length == 3
? "0" + royalHIstory.RoyalIns.ToString()
: royalHIstory.RoyalIns.ToString())
Vous pouvez également utiliser la fonction PadLeft
where instruct.InstructorInstrNo == royalHIstory.RoyalIns.ToString().PadLeft(4, '0')
ou simplement (en supposant que RoyalIns est un type entier)
where instruct.InstructorInstrNo == royalHIstory.RoyalIns.ToString("0000")
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots