1

I am maintaining a project and have come across some code which I can't understand. LINQ query:

var toDraw = from tile in testArr.AsEnumerable() where tile.Item_Business_Unit != null ? ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) && ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || (tile.Sales_Code.ToString() == cpg)) && (tile.Unit_Price != 0)) : ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) && ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || (tile.Sales_Code.ToString() == cpg)) && (tile.Unit_Price != 0)) select tile; 

From what I understand, from an array a tile is being selected which has the following criteria:

  • Ending date can be datetime.now or datetime.minvalue
  • Sales code can be null or can be equal to customer no or cpg
  • Unit price should be greater than 0

But I am not understanding why there is a conditional expression after tile.Item_Business_Unit since both of the conditions perform the same thing. So will the item be selected even if it has a null business unit? And does this work different from normal if/else operations?

Any suggestions would be very appreciated.

4
  • Looks like a relic. At some point, tile.Item_Business_Unit was used in the query, but it isn't anymore - the ternary just remained since the guy who made the change didn't bother removing it. Commented Oct 15, 2015 at 12:32
  • @Luaan thanks. so it means both perform the same thing. Just wanted confirmation because it is a project critical to the business and I cant mess with it. Thanks. Commented Oct 15, 2015 at 12:34
  • Well, if you want to be absolutely sure, have a look at the code for Item_Business_Unit - it's possible it has a side-effect you could be removing. It would be rather sneaky (and is the kind of offence you'd kill the other guy for :P), but it's possible. If it has no side-effects, you can remove it safely. Commented Oct 15, 2015 at 12:36
  • Thanks for your time @Luaan and sorry for the bother. Commented Oct 15, 2015 at 12:45

1 Answer 1

3

Are you being thrown by the shortcut notation?

x = (test_case) ? (true_part) : (false_part); 

If test_case evaluates to true, you would have

true_part

Whereas if test_case evaluates to false, this expression would be evaluated

false_part

UPDATE:

As an FYI: The resulting test of both sides of that conditional expression above are equal, so that cryptic code is not even necessary.

You could replace that with this:

var toDraw = from tile in testArr.AsEnumerable() where ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) && ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || (tile.Sales_Code.ToString() == cpg)) && (tile.Unit_Price != 0)) select tile; 
Sign up to request clarification or add additional context in comments.

5 Comments

Right, so its the same thing. Sorry for bothering the community with a stupid question. Was just confirming.
I ask a lot of stupid questions on here. Sometimes, you just don't know.
I keep looking at the two sides to that conditional expression in the screenshots. They look identical. Did you add a typo to the original? If not, you might be able to remove that cryptic test. It would make the query more readable to all other software developers.
No. It's not a typo. I found this code in a project I am maintaining and was scratching my head looking at the left part and then the right part again and again just like you and was not understanding why he'd use similar conditions. Then I just got frustrated and asked to have a third opinion on this. :D
Well, I hate to say it, but leave it as is. Others in the group (I've learned) get really upset when you change code that has been working for years. You might be able to put a comment in there saying how you think it should be written.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.