I currently have data in db table as follows:
Data in table
Desc Value
BNo 12
CNo Null
ANo 15
DNo Null
ENo 15
If ANo is blank i need to display BNo. If BNo is blank then display CNo on form. Once i found a non null value based on a defined priority order of DESC(In this case A-E), i need to exit my method. How can this be implemented,i can hard code desc names based on priority but it am looking to implement in a more optimized way. Any advices please.
It sounds like you have the means to order the list, as you state “in a priority order”. Not sure what this priority is but for the example let’s assume it is in alphabetical order by “Desc”. And let’s also assume (for simplicity) that these values are in a dictionary. Given this you can write a Linq statement as such…
eg…
This will return the first non-null value. If all values are null then null is returned.
There may be some incorrect assumptions here but the OrderBy and FirstOrDefault should be able to be used in whatever manner necessary to achieve your goal. The only things that should need to change is the “o.Key” and “d.Value” to reference your actual properties.
For example, if this information is in a class such as this…
and you have a list of these such as…
then you could write…