.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Querying Non- IEnumerable collections : LINQ to Object Part 4

Posted By:Dhananjay Kumar       Posted Date: March 24, 2010    Points: 25    Category: LINQ    URL: http://www.dotnetspark.com  

Querying Non- IEnumerable collections : LINQ to Object Part 4. In this article, I am going to show, how we could apply LINQ to query non-IEnumerable Collections.
 

Objective

In this article, I am going to show, how we could apply LINQ to query non-IEnumerable Collections.

I have created a class for my explanation purpose. Student class is having details of students.

Student.cs

namespace LINQtoOBJECT1
{
 public  class Student
  {
  public int RollNumber { get; set; }
  public string Name { get; set; }
  public int Section { get; set; }
  public int HostelNumber { get; set; }
  }
}

To create ArrayList of Student, I am creating a static function. This static function will return ArraList of student.

Function to return ArrayList of students.

public static ArrayList  GetStudentAsArrayList()
{
  ArrayList  students = new ArrayList
  {
 new Student() { RollNumber = 1,Name ="Alex " , Section = 1 ,HostelNumber=1 },
  new Student() { RollNumber = 2,Name ="Jonty " , Section = 2 ,HostelNumber=2 },
  new Student() { RollNumber = 3,Name ="Samba " , Section = 3 ,HostelNumber=1 },
  new Student() { RollNumber = 4,Name ="Donald " , Section = 3 ,HostelNumber=2 },
  new Student() { RollNumber = 5,Name ="Kristen " , Section = 2 ,HostelNumber=1 },
  new Student() { RollNumber = 6,Name ="Mark " , Section = 1 ,HostelNumber=2},
  new Student() { RollNumber = 7,Name ="Gibbs " , Section = 1 ,HostelNumber=1 },
  new Student() { RollNumber = 8,Name ="Peterson " , Section = 2 ,HostelNumber=2 },
  new Student() { RollNumber = 9,Name ="collingwood " , Section = 3 ,HostelNumber=1 },
  new Student() { RollNumber = 10,Name ="Brian " , Section = 3 ,HostelNumber=2 }
  };
  return students;
}

Now when I tried to query the returned ArrayList using LINQ, I got compile time error. Because ArrayList does not implement interface IQueryable and it is not IEnumerable collection.

Below code gave a compile time error

class Program
{
  static void Main(string[] args)
  {
  ArrayList lstStudents = GetStudentAsArrayList();
  var res = from r in lstStudents select r; 
  Console.ReadKey();
  }
}

Error is as shown below, 

1.gif

Above error cause because LINQ by default only query against IEnumerable collections.

So , how to query an ArrayList? Here Range variable comes into action.  If I modify the above query as below ,  I would be able to query LINQ against a non IEnumerable collection.

class Program
{
  static void Main(string[] args)
  {
  ArrayList lstStudents = GetStudentAsArrayList();
  var res = from  Student  r in lstStudents select r;
  foreach (Student s in res)
  {
 Console.WriteLine(s.Name);
  }
  Console.ReadKey();
  }
}

I am using Student as range variable. And we will get output as below .In above code I am using Student the class in ArrayList as range variable. 

2.gif

Conclusion

In this article, I discussed how to query against non IEnumerable collection.  Thanks for reading.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend