DCSIMG
Max() => Sequence contains no elements - שלמה גולדברג (הרב דוטנט)

שלמה גולדברג (הרב דוטנט)

מרצה בסלע ויועץ בעולם ה - net.

Max() => Sequence contains no elements

 

קריאה לפונקציית Max או פונקציית Min כשהמערך ריק תתרסק עם השגיאה "Sequence contains no elements"

 
 
אף פעם לא הבנתי למה אין MaxOrDefault עד שלבסוף מצאתי את הפוסט הבא (שמביא את הקוד הבא)
 
 

public static int MaxOrDefault<TSource>(this IEnumerable<TSource> source,

                                        Func<TSource, int> selector,

                                        int defaultValue)

{

    if (source.Any<TSource>())

        return source.Max<TSource>(selector);

 

    return defaultValue;

}

פורסם: May 16 2010, 12:33 PM by Shlomo | with no comments
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 8 and 5 and type the answer here:


Enter the numbers above: