Friday 28 September 2018

How To Overcome "Sequence contains more than one element"

Agenda :
            Dear all In this article i would like to demonstrate How To Overcome "Sequence contains more than one element".This example was demonstrated with the help of C#.Net using Console Applications.

Description :
          In previous articles I explained different articles related to C#.Net,Asp.Net,Sql Server,XML and DotNetInterviewQuestions Related articles.Let's know how we will work with Work with How To Overcome "Sequence contains more than one element"




RootCause Of this Error:
          If we are trying to read the data from a list by using SingleOrDefault() if there is more than one record of data in the list it leads to the Sequence contains more than one element error.





Difference Between SingleOrDefault() VS FirstOrDefault():


                        SingleOrDefault()
                            FirstOrDefault()
1.If we confirmed that our Result Set Returns Exactly Single( 1 ) Result then this method is an suggestable approach
1.If we confirmed that our Result set Returns List of Results then FirstOrDefault() method is an suggetstable approach
2.If Our Result set returns 0 records then it returns the default value for the type (For E.G default Value of Bool is false)
2.If Our Result set returns 0 records then it returns the default value for the type (For E.G default Value of Bool is false)
3.If Our Result set returns 1 records then it returns that Record
3.If Our Result set returns 1 records then it returns that Record
4.If Our Result set returns many records it throws an exception named as "Sequence contains more than one element" because SingleOrDefault() can has an capability to accomidate Single Record if it gets too many records it raises an exception
4.If Our Result set returns many records then it returns First Record from the result set
5.It assumes that there is a single item and returns it or default if none exists
5.It returns First Item from the multiple list
6.Performance : FirstOrDefault() is usually faster because it fetches Top(1) from the list
6.Performance : SingleOrDefault() is an costliest approach because it always checks for a single(One) element has to iterate the whole enumerable when it doesn't find.
7.Conclusion : If you ensure that every time Single Element will result no matter of the condition then We can use SingleOrDefault()
7.Conclusion : If we always want First Record from List irrespective of the items then FirstOrDefault() is an best choice.

Program:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeqDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<DemoClass> obj = new List<DemoClass>();
            obj.Add(new DemoClass() { Id = 1, Name = "Veerendra" });
            obj.Add(new DemoClass() { Id = 1, Name = "Veerendra" });
            obj.Add(new DemoClass() { Id = 2, Name = "Naga" });
            Console.WriteLine(obj.Where(p => p.Id == 1).SingleOrDefault().Name);//Error
            Console.WriteLine(obj.Where(p => p.Id == 1).FirstOrDefault().Name);
            Console.ReadLine();
        }
    }
    class DemoClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

No comments :

Post a Comment