.NET's Linq revolutionary enhanced the ability of developers to write simple and efficient code. However, the choice between smooth grammar and inquiries of expression and grammar may be just a preference for some people. Let us explore their respective advantages to clarify their applicable scenarios.
Inquiries in the inquiries in complex query
When processing complex queries involving multiple range variables, the query grammar performs well. This situation appears in the following scene:
Use Keyword
let
from
Consider the following example: Smooth grammar: simplicity and flexibility
string[] fullNames = { "Anne Williams", "John Fred Smith", "Sue Green" }; var query = from fullName in fullNames from name in fullName.Split() orderby fullName, name select name + " came from " + fullName;
Simple inquiries that do not need high -level functions, smooth grammar is better. It provides a more concise and direct way to express your inquiries, especially when dealing with less complicated operations. In addition, smooth grammar can access all query operators.
For example: <例> <合> The advantages of the two methods
You can combine query and method grammar to use their respective advantages. This is very common in Linq to SQL query:
var query = fullNames .SelectMany(fName => fName.Split().Select(name => new { name, fName })) .OrderBy(x => x.fName) .ThenBy(x => x.name) .Select(x => x.name + " came from " + x.fName);
In the end, the choice between smooth grammar and query expression depends on the nature of the query. For complex queries involving multiple range variables, the query expression provides flexibility and readability; and for the simpler query, the smooth syntax is better, and it provides access to all query operators.
The above is the detailed content of Fluent vs. Query Expression in LINQ: When Should I Use Which Syntax?. For more information, please follow other related articles on the PHP Chinese website!