![]() “Saving” values for later in the query (‘let’ keyword)Īs I said in the beginning, there’s no distinct advantage of one syntax over the other besides readability. The LINQ feature manipulates a collection of data using methods, called operators, that sort, filter, select, project, aggregate, partition, and so on.LINQ extensions methods released as a sample with Visual Studio 2008. In some special cases, like the ones described above, query-syntax can be better. Suppose you want to write a LINQ to SQL or Entity Framework query that implements. ![]() Even if you get used to it, remember that most programmers still find method-syntax more readable. You can switch to query-syntax for those, but I don’t recommend it. It provides an easy-to-understand functional syntax. In most cases, with simple operators like Where, Select and OrderBy the method-syntax is great. And by better I mean more readable and easier code. Join prod in products on category.ID equals prod.CategoryIDĪs a rule of thumb, whenever you need to Join collections, the query-syntax is going to be better. But unlike in query-syntax, it’s not really clear from the code. In the method-syntax way, the 2nd and 3rd parameters of Join are compared. It allows to store a result for later use in the query. These methods perform equijoins or joins that match two data sources based on equality. One of the nicest things about the query-syntax is the let keyword. The join methods provided in the LINQ framework are Join and GroupJoin. You’ll see some cool examples where method-syntax creates pretty complicated code that can be replaced by a short and neat query-syntax expression. By better, I mean it makes more readable code. There are several cases where query syntax is better, and this is what this post is all about. ![]() However, method-syntax isn’t always a better syntax. Dynamic LINQ Entity Framework (Core) Client vs.You can use any LINQ method that support predicate with a dynamic C expression: Deferred.There was a library. In fact, any query syntax can be transformed into method syntax (See Dudi Keleti’s There isn’t any distinct advantage of one over the other. Unlike query-syntax, which is sort of a new language (well, kind of like SQL but still much less familiar than C#). I can’t explain it fully, but I think the reason is that programmers are used to regular method calls. Var numbers = Enumerable.Range( 1, 100) //1,2.,100įor some reason, most developers (including myself) feel much more comfortable with Method Syntax. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |