-1

I'm a little (very) confused about the usage of the the following queries

This:

var query = from p in ctx.Persons
            where x.Flat.Building.Id == 1 && x.Archived == false
            select p;

And this:

var query = ctx.Persons.Where(x => x.Flat.Building.Id == 1 && x.Archived == false);

If I make some changes to the result of both queries and try to query them again, the 1st shows the old results, but the 2nd doesn't?

This answer tries to explain, but it is really confusing, at least for me.

EDIT Maybe my question doesn't be so clear as I expect but I'm going to try to focus.

My exact problem is if I use Query Sintax and made some changes over the data, and query again it doesn't load the fresh data. But in This answer The autor talks about if use Fluent Sintax this doesn´t happen.

Now here is my nigthmare why this happens with Query Sintax and doesn't with Fluent sintax?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
  • [LINQ - Fluent and Query Expression - Is there any benefit(s) of one over other?](http://stackoverflow.com/questions/214500/linq-fluent-and-query-expression-is-there-any-benefits-of-one-over-other) – Tim Schmelter Aug 24 '14 at 21:48
  • it depends on the query you are doing,both present the same outcome,one with query syntax and the other with fluent syntax\dot notation.Especifically query syntax shines in readability is with joins and transparent identifiers – terrybozzio Aug 24 '14 at 21:55
  • About your update: The syntax query you've posted will be transformed to the exact set of method calls as the method-based query when you compile your code ([C# specification](http://msdn.microsoft.com/en-us/library/ms228593.aspx) **7.16 Query expressions**). Query syntax is just a syntactic sugar for method calls. Are you sure you don't have additional `ToList()` or `ToArray` call somewhere, which would make your query evaluate results, store them in a list/array and return the same set of results when you access that variable again? – MarcinJuraszek Aug 24 '14 at 23:29
  • Yes, I have **ToList()** But it is called directly from the query. I'm was Reading something about **DbContext** and some commands like '**Find()** allways return a chache instance of the entity (if it exists) instead of getting it from the database. – Juan Pablo Gomez Aug 25 '14 at 02:23
  • If that was the case you'd see the exact same result for both queries, because hey are equivalent after compilation. – MarcinJuraszek Aug 25 '14 at 02:28
  • Really i don't know and thats why I'm posting the question. The only reference to this I'm talking about is **"have you tried something like: ctx.Persons.Where(x => x.Flat.Building.Id == 1 && x.Archived == false); ===== EDIT ===== In this case I think you approach is, imho, really hazardous. Indeed you works on the data loaded by EF to interpret your query rather than on data resulting of the interpretation of your query. If one day EF changes is loading policy (for example with a predictive pre-loading) your approach will "send you in then wall". "** posted here in stackoverflow. – Juan Pablo Gomez Aug 25 '14 at 02:33

1 Answers1

4

The first query it is exactly the same with the second query. The only difference between them is the syntax. In the first one you have the so called query syntax and in the second one you have the so called fluent (or method) syntax. Under the hood the first query, will be complied at the first step of complilation to the second query.

For more information about this, please have a look here. As it is stated there,

Query syntax and method syntax are semantically identical

Christos
  • 53,228
  • 8
  • 76
  • 108