i went over some questions and searched google a bit,
but i couldnt find an answer ( That satisfies me ).
Basicly, i understand the SingleOrDefault return null or 0 ( depends on the type ).
but how can i make it return something else ?
return myChannels.All.Where(_Channel => _Channel.Guid == this.ParentChannelGuid).SingleOrDefault(_SPECIFICCHANNEL);
so, i want _SPECIFICCHANNEL to be returned in case it is not single..
can that be done ?
This can be accomplished in a rather simple way. If you create your own extension method that is more specific than the generic
SingleOrDefault, then the compiler will prefer the more type-specific version. Here’s an example that shows how to do that with a simplePersonclass (you can copy-paste it into LINQPad to quickly see the result):In the above example,
SingleOrDefault(IEnumerable<Person>), takes precedence overSingleOrDefault<T>(IEnumerable<T>)which is less specific.