AsEnumerable() in Linq

AsEnumerable() method in Linq is used to cast or convert given type into it's IEnumerable type. AsEnumerable() basically changes the compile time type of given type that implements IEnumerable to IEnumerable itself.

Sample code to use AsEnumerable() in Linq

 protected void Page_Load(object sender, EventArgs e)
  {
      var numArray = new int[5];
      numArray[0] = 5;
      numArray[1] = 10;
      numArray[2] = 7;
      numArray[3] = 9;
      numArray[4] = 1;

      var numbers = numArray.AsEnumerable();
      foreach (var number in numbers)
       {
          Console.WriteLine(number);
       }
  }

Related Posts:

  • Convert ToArray in linqIn C# while using Linq, we have .ToArray() extension method that is used to convert IEnumerable to an array. .ToArray() forces immediate query execut… Read More
  • AsEnumerable() in LinqAsEnumerable() method in Linq is used to cast or convert given type into it's IEnumerable type. AsEnumerable() basically changes the compile time typ… Read More
  • Convert ToList() in LinqIn C# Linq, there is .ToList() extension method that is used to convert IEnumerable to List of type. Like .ToArray(), ToList() also forces immediate q… Read More
  • Take() in Linq C#In Linq i.e. System.Linq, Take operator is used to get the first specified number of elements a sequence. Linq Take() example using System.Linq; pro… Read More
  • Skip in Linq C#Skip() in Linq avoids or skips the given number of elements from the sequence. Skip() example in Linq protected void Page_Load(object sender, E… Read More

0 Comments :

Post a Comment