0

I have a weird situation going on. I have following query using Devexpress XPO to MS SQL.

var _elements = (from n in new XPQuery<Elements>( XpoDefault.Session )
                 select n).Take(100);

int count =  _elements.Count();

foreach ( var e in _elements) {
     Display(e);
);

When query has 0 results Count() returns 100, but foreach statement is not executed, because enumerator yield no results.

How can I check how many rows are in query, and take only 100 records (or less)?

I don't understand why Count() returns number from take, while there no records to enumerate.

I hope that you can help me. Thanks!

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
GigaKatowice
  • 551
  • 1
  • 6
  • 14
  • 4
    What if you add `.ToList()` after the Take(100) ? You are now executing the query twice. – Hans Kesting Nov 27 '14 at 14:48
  • 4
    What is `XPQuery`? Does it support multiple enumerations? Is the foreach loop executed if you remove the previous statement (the one with `Count()` ? – Thomas Levesque Nov 27 '14 at 14:48
  • @ThomasLevesque - I don't know. I can point to documentation, if it help - https://documentation.devexpress.com/#XPO/clsDevExpressXpoXPQuery~T~topic – GigaKatowice Nov 27 '14 at 15:04
  • @GigaKatowice Can you answer Thomas' second question? "Is the foreach loop executed if you remove the previous statement (the one with `Count()`?" And Hans Kesting's question as well. – dcastro Nov 27 '14 at 15:11
  • @HansKesting - Creating query with .Take(100).ToList() - returns correct count, and foreach statement is not executed. Acctual query to server is executed only once. When I use only Take(100), there are two queris to the server, one for Count(), and second form Take(100). So probably it is a problem with Devexpress lib. – GigaKatowice Nov 27 '14 at 15:12
  • @ThomasLevesque - when I remove the line with Count(), the foreach statement is not executed – GigaKatowice Nov 27 '14 at 15:18
  • 1
    @GigaKatowice If the foreach statement is never executed, then the `_elements` simply contains no elements. If that is so, `Take(n).Count()` should always return 0. That seems like a bug in the LINQ provider you're using. – dcastro Nov 27 '14 at 15:31
  • 1
    Is the `Take` in your code the `Enumerable.Take` extension? – dcastro Nov 27 '14 at 15:32
  • 1
    @dcastro, I think it's the `Queryable.Take` extension method, so the implementation depends on the provider. It looks like the provider wrongly assumes that `Take(100)` will always return 100 items. – Thomas Levesque Nov 27 '14 at 22:44
  • @GigaKatowice, I suggest you post your problem to the DevExpress forums, because it looks like a bug in their XPO product. – Thomas Levesque Nov 27 '14 at 22:45
  • @ThomasLevesque my thoughts exactly – dcastro Nov 27 '14 at 23:10
  • Thank you for help. You are right. Take() is IQueryable.Take. I posted my problem to Devexpress forums. Again, thanks for help! – GigaKatowice Nov 28 '14 at 07:35

1 Answers1

0

If you want to check how many rows are in query you need to execute Count() before Take(). Take returns IEnumerable, it means, that query will execute in moment, when you will execute ToList, First/FirstOrDefaut, Last/LastOrDefault etc. More information about IEnumerable

Community
  • 1
  • 1
SWol
  • 48
  • 6
  • 1
    If think you don't understand what is the real problem. I want to know how many records I actually take from database (from 1 to 100 max). The problem is that, Count() after Take() always returns 100. – GigaKatowice Nov 27 '14 at 15:19