c# - Weird LINQ behaviour -


I have this code:

Here I have been given a long list from the database:

  IQueryable & LT; Long & gt; Select t = from the query = to the table. LongID  

And here I try to get the most from those IDs:

  long max = query. anyone () ? Query.Max (): 0;  

But no matter that the result of the question does not take much time, the maximum is always set to 0.

Why do you have any idea?

if

  long max = query.Any ()? Query.Max (): 0; Returns zero, then one of the following is true:  
  1. The query does not return any results
  2. In the query results The maximum value is zero

    The first case is possible when you modify your table between defining the query and getting the maximum value from the query. Remember - query does not have any data. This is the only query definition, and when you execute the query, you will receive the data (eg any call) or maximum ()).

    Test:

      list & lt; Long> Table = new list & lt; Long & gt; {1, 2, 3}; Var query = Select T in the table; // query did not execute the table. clean (); // modify the data source before executing the query Assert.False (query.Any ()); // execute query on modified data source  

Comments