Am I using the operator ?? to try to assign an object based on the best match found in the list.
I have various matching rules, but simplified this for an example:
objectImTryingToSet = MyListOfPotentialMatches.FirstOrDefault(*/lamda checking numerous things*/) ?? //rule1 MyListOfPotentialMatches.FirstOrDefault(*/lamda checking different numerous things*/) ?? //rule2 MyListOfPotentialMatches.FirstOrDefault(*/lamda checking again different numerous things*/); //rule3
For debugging purposes, I would like to save the record in a line, which rule was the one that successfully assigned objectImTryingToSet , since I have an error when in one script an object is assigned, when it should not be, and its real headache manually tries to sift all of these rules to find out where the wrong assignment is located.
So I basically want a pseudo:
string ruleThatMatched = null; objectImTryingToSet = MyListOfPotentialMatches.FirstOrDefault(*/lamda checking numerous things*/) ?? if (objectImTryingToSet != null) { ruleThatMatched = "rule1" } //rule1 MyListOfPotentialMatches.FirstOrDefault(*/lamda checking different numerous things*/) ?? if (objectImTryingToSet != null) { ruleThatMatched = "rule2" } //rule2 MyListOfPotentialMatches.FirstOrDefault(*/lamda checking again different numerous things*/); if (objectImTryingToSet != null) { ruleThatMatched = "rule3"} //rule3 //tried all the rules and still no match if (objectImTryingToSet == null) { ruleThatMatched = "no rule managed to find a match"; }
Is this possible with the operator ?? ?
source share