Solved

Counting records using LINQ

Posted on 2010-11-12
4
694 Views
Last Modified: 2013-11-11
HI

I'm new to LINQ, so I'm guessing my issue is probably pretty basic;

I've created a new datacontext and have executed a query on it.

i.e;

SomeDataContext dc = new SomeDataContext(connString);

IEnumerable<Reading> readings =dc.ExecuteQuery<Reading>(SQL, thing1, thing2);

int count_readings = readings.Count();

returns the error
"The query result cannot be enumerated more than once"

I could get the number of records via:

count_readings = (from thing1 in dc.Readings select thing1).Count();
but thats not very useful!

Help much appreciated!


0
Comment
Question by:Spike_66
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
According to http://stackoverflow.com/questions/168901/howto-count-the-items-from-a-ienumerablet-without-iterating, Count() is not "truly" implemented for IEnumerable, even though it appears as a method of the object. If you need a count, the suggestion is to return your objects into either a ICollection or IList as both of them support Count(). Otherwise, you would need to iterate through your IEnumerable to find the count.
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 125 total points
Comment Utility
Try this:

var readings =dc.ExecuteQuery<Reading>(SQL, thing1, thing2).ToArray();
int count_readings = readings.Length;

DaTribe
0
 

Author Closing Comment

by:Spike_66
Comment Utility
Excellent and simply explained
0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
Glad to help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

14 Experts available now in Live!

Get 1:1 Help Now