?
Solved

How to read the resultset from LINQ to IEnumerable

Posted on 2010-09-09
3
Medium Priority
?
344 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
[X]
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
  • 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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