2

I am using Linq like this:

IQueryable<Rentals> rentals = from r in context.Rentals
                              select r;


foreach (Rentals r in rentals)
{
    str +=  r.ListingID + "|";             
}

but str has only 50 records while, if I do rentals.Count(), it shows 1700. I tried to debug and saw that the flow of control goes out of the foreach loop after the 50th record. Why is that so?

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
  • 6
    well, it can't be **both** LINQ-to-SQL **and** EF... which is it? Also: not key to the question, but you should look at `StringBuilder` here (for the concatenation-in-a-loop etc) – Marc Gravell Nov 28 '11 at 14:49
  • 2
    Don't have the answer, but consider using `StringBuilder` when doing string concatenation operations in a loop. – Anthony Pegram Nov 28 '11 at 14:49
  • @AnthonyPegram : it is not concatenation issue. control exits after 50 iterations. It can be time out issue. as rentals are picked from service ? – DotnetSparrow Nov 28 '11 at 14:51
  • I would say consider using `string.Join` rather than `StringBuilder` here – Snowbear Nov 28 '11 at 14:52
  • 1
    @DotnetSparrow: They aren't saying it's a concatenation issue, that's why they posted as comments instead of answers. They are just saying there are more efficient ways to concatenate then using pure string concatenation. – jason Nov 28 '11 at 14:53
  • Do you have a try/catch around this code? – Wouter de Kort Nov 28 '11 at 14:56
  • @Jason: I first tried stringbuilder but was checking if it is stringbuilder's length issue that's why i used string. thanks for clarification – DotnetSparrow Nov 28 '11 at 14:57
  • 3
    try doing foreach (Rentals r in rentals.ToList()) which will force the query execution and see how many records you got – Wojtek Turowicz Nov 28 '11 at 14:57
  • 2
    Try `var str = string.Join("|", rentals);` instead of doing the looping yourself. By the way, the error could be due to timeout issues with the database. Have you tried `rentals.ToArray()`? – Anders Marzi Tornblad Nov 28 '11 at 15:00
  • Can you post the query run on both the count and the foreach loop, you can get this using sql profiler thanks – Eva Lacy Nov 28 '11 at 15:05
  • Instead of string.Join or using a StringBuilder use `rentals.Aggregate("", (all,n)=>all + n.ListingID + "|")` – edvaldig Nov 28 '11 at 15:09

4 Answers4

1
List<Rentals> rentals = (from r in context.Rentals
                              select r).ToList();

Try creating a list first and check if that works. Also, use a StringBuilder() to build. Let me know if this works.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • I used above ToList and I get rental.count = 50, but If I try to use without ToList, It gives 1362 record count but shows 50. whats is error ? – DotnetSparrow Nov 28 '11 at 15:13
0

Try this:

List<Rentals> rentals = context.Rentals.ToList();

str = Enumerable.Aggregate(rentals, str, (current, r) => current + (r.ListingID + "|"));

It's more efficient that the foreach loop.

Ed B
  • 6,028
  • 3
  • 26
  • 35
0

Are you able to change your code to be the following?

IEnumerable<Rentals> rentals = from r in context.Rentals
                               select r;
Mark
  • 1,884
  • 2
  • 25
  • 36
0

If you use IQueryable<T>, your results will be first prepared as an sql statement (using expression trees) and then executed and would be processed out-of-process (server side), but if you use .ToList() on the result or use IEnumerable<T> return type - your results would be executed using LINQ to Objects (in-memory) which is what Count is doing.

Try to check the Expression property of rentals variable when using IQueryable<T> as return value. I am still not sure why the number of records are different, but the below links explain the difference and the usage of IQueryable<T> and IEnumerable<T>:

http://blogs.msdn.com/b/erickt/archive/2006/10/23/iqueryable-t-vs-ienumerable-t.aspx

http://jonkruger.com/blog/2007/10/19/iqueryable-vs-ienumerable-in-linq-to-sql-queries/

Returning IEnumerable<T> vs. IQueryable<T>

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65