• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

How to read the resultset from LINQ to IEnumerable

              IEnumerable<FundsReceiptBatchDetails> batchIds =
                   (from frBatchids in lstfrbd select new { frBatchids.FundsReceiptBatchID }).Distinct().ToList() as

I am getting exception like object reference is not set to instance of the object.
  • 2
1 Solution
Bob LearnedCommented:
There are a few possibilities.  I am confused, though, why you would create a generics List, and thant convert it back to IEnumerable, which is what form it was in before the ToList call.  I prefer to work with List<FundsReceiptBatchDetails> myself.  
Fernando SotoRetiredCommented:
Hi kravindra;

To your question, "How to read the resultset from LINQ to IEnumerable", When you query a database the result set is returned as a IQueryable collection. Because IQueryable inherits from IEnumerable all you need to do is to cast it to a Enumerable by using AsEnumerable method. Then to execute the query and get the result set you must enumerate over the query by using either a foreach loop or applying a method on the query which will enumerate over it such as ToList, ToArray, Count and others.

I would suggest to change your query as follows :

var batchIds = (from frBatchids in lstfrbd
                select frBatchids.FundsReceiptBatchID
Then enumerate over the query like so :

foreach( var ids in batchIds )
    // Do what you need with each ID.
    Console.WriteLine("Value if Id = " + ids.FundsReceiptBatchID);

In the above query batchIds is of type IEnumerable collection of whatever type FundsReceiptBatchID is in the database.

Fernando SotoRetiredCommented:
Hi kravindra; did this work for you?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now