Solved

How to read the resultset from LINQ to IEnumerable

Posted on 2010-09-09
3
331 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 62

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 62

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setting location of a form 4 22
Default parameter problem in C# 3 27
Windows Service to Receive TCP Packets 4 43
Birthdays 3 14
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…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now