SingleOrDefault() throws an exception on more than one element

后端 未结 7 1908
野性不改
野性不改 2021-02-05 12:17

I\'m getting an exception whenever I fetch like this

Feature f = o.Features.SingleOrDefault(e => e.LinkName == PageLink);

because this can r

相关标签:
7条回答
  • 2021-02-05 12:19

    Single and SingleOrDefault are designed to throw if more that one match exists in the sequence. A consequence of this is that the entire sequence must be iterated prior to completion. It does not sound like this is what you want. Try FirstOrDefault instead:

    Feature f = o.Features
        .FirstOrDefault(e => e.vcr_LinkName == PageLink && e.bit_Activate == true);
    

    This will (generally) perform better because it completes as soon as a match is found.

    Of course, if you actually want to retain more than one element, a Where clause would be more appropriate:

    IEnumerable<Feature> fs = o.Features
        .Where(e => e.vcr_LinkName == PageLink && e.bit_Activate == true);
    
    0 讨论(0)
  • 2021-02-05 12:23

    SingleOrDefault suggests that you are expecting 0 or 1 results from your query. If you have more than 1 then there is something wrong with your data or query.

    If you are expecting more than 1 result and only want the first one, then FirstOrDefault should be used.

    0 讨论(0)
  • 2021-02-05 12:25

    Alternatively, if you only want the item when there is exactly one match and do not want to throw when there are more than one, this can be easily accomplished. I've created an extension method for this in my project:

    public static class QueryableExtensions
    {
        public static TSource SingleWhenOnly<TSource>(this IQueryable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
    
            var results = source.Take(2).ToArray();
    
            return results.Length == 1 ? results[0] : default(TSource);
        }
    
        public static TSource SingleWhenOnly<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (predicate == null)
                throw new ArgumentNullException("predicate");
    
            var results = source.Where(predicate).Take(2).ToArray();
    
            return results.Length == 1 ? results[0] : default(TSource);
        }
    }
    
    0 讨论(0)
  • 2021-02-05 12:38

    If you are using SingleOrDefault if the condition satisfy more than result it will throw error.

    you can achieve your result by using FirstOrDefault

    0 讨论(0)
  • 2021-02-05 12:39

    If you only want the first element, use FirstOrDefault instead.

    Basically, here are the options in terms of valid results (i.e. where you don't want to throw) and what to use:

    • Exactly one: Single
    • One or zero: SingleOrDefault
    • One or more: First
    • Zero or more: FirstOrDefault

    (ElementAt and ElementAtOrDefault, Last and LastOrDefault are also available.)

    0 讨论(0)
  • 2021-02-05 12:39

    Single means that you expect be one element in the sequence. SingleOrDefault means that you expect there to be one or zero elements in the sequence. This should be used when you want know there is one (or zero) and you want it to crash when more than one it returned.

    If you are after just one, use First (or FirstOrDefault) as suggested above, but make sure you order the data correctly.

    0 讨论(0)
提交回复
热议问题