Good day
I have been trying to use a linq to entity SelectMany() but always throw "object reference not set to an instance of an object" exception. thougth may be one of the elements of the sequence was null. Therefore i try to extend(write an extension method) for the SelectMany() as SelectManyOrDefault() to check is there is any null element and skip such but i was still getting the same "object reference not set to an instance of an object" exception. Try to set break points debug my project but to my surprise none of the elements at any point is null or empty. The exception is been thrown on the "yeild return item". This is the extension method
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static IEnumerable<TR> SelectManyOrDefault<TS, TR>(this IEnumerable<TS> source, Func<TS, IEnumerable<TR>> selector)
- {
- if (source == null)
- throw new ArgumentNullException("source");
-
- if (selector == null)
- throw new ArgumentNullException("selector");
-
-
-
-
-
-
-
- foreach (TS element in source.DefaultIfEmpty())
- {
- if (element != null)
- {
- foreach (TR item in selector(element).DefaultIfEmpty())
- {
- if(item != null)
- yield return item;
- }
- }
- }
-
-
- }
Please help me, I don't know what is actually cause this exception.
I have been on this for a week now.