For brevity's sake in my code, i'd like to be able to do the following: having a collection, find the first element matching a lambda expression; if it exists, return the value of a property or function. If it doesn't exist, return null.
Updated examples w. classes
Let's have a collection of stuff
class Stuff
{
public int Id { get; set; }
public string Value { get; set; }
public DateTime? ExecutionTime { get; set; }
}
What I am aiming for is a way to return nicely when calling this
var list = new Stuff[] { new Stuff() { Id = 1, Value = "label", ExecutionTime = DateTime.Now } };
// would return the value of ExecutionTime for the element in the list
var ExistingTime = list.FirstOrDefault(s => s.Value.Contains("ab")).ExecutionTime;
// would return null
var NotExistingTime = list.FirstOrDefault(s => s.Value.Contains("zzz")).ExecutionTime;
Is it possible with some linq-syntax-fu or do I have to check explicitly for the return value before proceeding?
Original example w. strings
var stuff = {"I", "am", "many", "strings", "obviously"};
// would return "OBVIOUSLY"
var UpperValueOfAString = stuff.FirstOrDefault(s => s.contains("bvi")).ToUpper();
// would return null
var UpperValueOfAStringWannabe = stuff.FirstOrDefault(s => s.contains("unknown token")).ToUpper();
Comment: I shouldn't have used strings in my original example, since it slightly skews the question by centering it on the ToUpper method and the string class. Please consider the updated example
Why not just do:
If you have a "non-default default", you can do:
Update:
Based on the question clarification, you don't need any extra code to achieve what you want. Simply use a where clause and a select projection clause:
Old Answer
As suggested in the comments (and made generic in another answer), wrap the
ToUpper
call in an extension method. Extension methods boil down to syntax sugar around static method calls, so they can handle nulls just fine.Your call would become:
It is now just a discussion over whether the extension method to simply support single-line style code is more expressive than multiple lines - at the end of the day, expressing the intent of the code is more important than what the code looks like.
Personally I think extension methods that handle nulls are confusing at first glance, as they are sugared up to look like regular methods.
I like this as an extension method:
And usage:
This will chain return the default value (
null
in this case, but as is correct for that type), or call the relevant function and return that.