Question

I have the line below but still get an exception "Sequence contains more than one element"

Details rd = this.db.Details.SingleOrDefault(x => x.TId == Id && x.TypeId == TypeId);

I was hoping that SingleOrDefault would avoid the exception.

Was it helpful?

Solution

SingleOrDefault returns a SINGLE element or null if no element is found. If 2 elements are found in your Enumerable then it throws the exception you are seeing. Just like Highlander... with Single - there can be only one.

FirstOrDefault returns the FIRST element it finds or null if no element is found. so if there are 2 elements that match your predicate the second one is ignored.

Assuming you don't care if there are multiple matches and you only want the first one or null if no match is found... then you probably want the following...

Details rd = this.db.Details
    .FirstOrDefault(x => x.TId == Id && x.TypeId == TypeId);

Note that both of these methods only return one element they only differ in what they do after they find a match. First stops looking at that point and returns what it found, Single keeps checking the rest of the list to make sure there are no more matches. The OrDefault part determines what it returns if no match is found. SingleOrDefault or FirstOrDefault returns null if no value is found but if you just use Single or First then it MUST find one match or it will throw an exception.

EDIT: Good point Steve Since First returns the first element you may need to use an OrderBy in order to make sure the element you want is indeed first. For instance... suppose your object had an UpdateDate property and you wanted the object with the most recent UpdateDate...

Details rd = this.db.Details
    .OrderByDescending(x => x.UpdateDate)
    .FirstOrDefault(x => x.TId == Id && x.TypeId == TypeId);

OTHER TIPS

In case you are having a list, convert the list to IEnumerable list, then you can make use of FirstOrDefault method

 IEnumerable<BuyOnlineSearchdetails> details = new List<BuyOnlineSearchdetails>();

var FirstRow = details.FirstOrDefault();
            string Count = "0";
            if (FirstRow != null)
            {
                Count = FirstRow.TotalCount.ToString();
            }
            else
            {
                Count = "0";
            }

You have to use FirstOrDefault() instead of SingleOrDefault().

SingleOrDefault throws an exception if more than one element exists where as FirstOrDefault not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top