Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to read the resultset from LINQ to IEnumerable

Posted on 2010-09-09
3
334 Views
Last Modified: 2013-11-11
              IEnumerable<FundsReceiptBatchDetails> batchIds =
                   (from frBatchids in lstfrbd select new { frBatchids.FundsReceiptBatchID }).Distinct().ToList() as
                   IEnumerable<FundsReceiptBatchDetails>;

I am getting exception like object reference is not set to instance of the object.
0
Comment
Question by:kravindra
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33643155
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.  
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33643579
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
               ).Distinct().AsEnumerable();
               
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
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33651144
Hi kravindra; did this work for you?
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question