Wednesday, July 31, 2013

In-line Conditionals

Sometimes it is nice to put a condition in line, without calling an external function or writing an in place if statement.

I am using Linq for entities, and I needed to assign different icons based upon database values.

I was able to use the code below:

select new LocationTree
                            {
                                Id = l.locationID,
                                Image = (true==l.LocationType.LocationHoldsInventory)?                                                    "Images/icons16px/plus.png":"Images/icons16px/redMinus.png",
                                ItemName = l.locationName,
                                HoldsInventory = l.LocationType.LocationHoldsInventory??false
                            };

This allowed me to use one image or the other depending upon the value of the LocationHoldInventory flag.

No comments:

Post a Comment