Solved

How to read the resultset from LINQ to IEnumerable

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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