Linq Expression Syntax - How to make it more readable?

Right now I am writing something that Linq will use to combine the results from my database through Linq2Sql and a list of objects in memory to find out which of my objects in memory correspond to something in the database.

I came up with a query in the expression and query syntax.

Expression syntax

var query = order.Items.Join(productNonCriticalityList, i => i.ProductID, p => p.ProductID, (i, p) => i); 

Query syntax

 var query = from p in productNonCriticalityList join i in order.Items on p.ProductID equals i.ProductID select i; 

I understand that we have every opportunity to complete the code with syntax of expressions, and I really use it more. Mostly because it’s easier to create reusable pieces of filter code that can be joined together to form more complex filters.

But for the connection, the latter seems to me more readable, but perhaps this is because I'm used to writing T-SQL.

So, did I miss the trick or is it just a matter of getting used to it?

+4
source share
5 answers

I agree with other respondents that the exact question you ask is just a matter of preference. Personaly, I mix two forms, depending on what is clearer for the particular query that I am writing.

If I have one comment, I would say that the request looks like it can load all the items from the order. This may be good for one order once, but if you focus on many orders, it may be more efficient to load all the items for the whole time (you may want to additionally filter by date or client or something else). If you do, you can get better results by switching the query:

 var productIds = (from p in productNonCriticalityList orderby p.productID select p.ProductID).Distinct(); var orderItems = from i in dc.OrderItems where productIds.Contains(i.ProductID) && // Additional filtering here. select i; 

A little back at first glance, but this can save you from loading in all elements of the order, as well as from sending a large number of requests. It works because calling where productIds.Contains(...) can be converted to where i.ProductID in (1, 2, 3, 4, 5) in SQL. Of course, you will have to judge this based on the expected number of order items and the number of product identifiers.

+1
source

In fact, it all comes down to preference. Some people simply hate the idea of ​​a query as syntax in their code. For one, I appreciate the query syntax, it is declarative and quite readable. As you said, however, the tenacity of the first example is a good thing. I believe that for my money I will keep asking until I feel that I need to start the call chain.

+1
source

I felt the same way. Now I find that query syntax is easier to read and write, especially when things get complicated. No matter how annoying it is to print it for the first time, let it do wonderful things in ways that could not be read in the syntax of expressions.

+1
source

I prefer the syntax of a query when its complex syntax and expression are expressed with its simple query.

If the DBA had to read C # code to see which SQL we use, they would better understand and simplify the query syntax.

Taking a simple example:
Inquiry

 var col = from o in orders orderby o.Cost ascending select o; 

Expression

 var col2 = orders.OrderBy(o => o.Cost); 

For me, Expression syntax is an easier choice to understand here.


Another example:
Inquiry

 var col9 = from o in orders orderby o.CustomerID, o.Cost descending select o; 

Expression

 var col6 = orders.OrderBy(o => o.CustomerID). ThenByDescending(o => o.Cost); 

Both are easy to read and understand, however if the request was

 //returns same results as above var col5 = from o in orders orderby o.Cost descending orderby o.CustomerID select o; //NOTE the ordering of the orderby's 

This looks a bit confusing as the fields are in a different order and it looks a little backward.


For associations
Inquiry

 var col = from c in customers join o in orders on c.CustomerID equals o.CustomerID select new { c.CustomerID, c.Name, o.OrderID, o.Cost }; 

Expression

 var col2 = customers.Join(orders, c => c.CustomerID,o => o.CustomerID, (c, o) => new { c.CustomerID, c.Name, o.OrderID, o.Cost } ); 

I believe the request is better.


My resume will use what looks easiest and fastest to understand, given this request. You cannot use the golden rule. However, if there are many connections, I would go with the query syntax.

+1
source

Well, both statements are equivalent. This way you can get around both of them, depending on the code and more readable code. In my project, I decide which syntax to use depending on these two conditions.

Personally, I would write the syntax of the expression in one line, but this is a matter of taste.

0
source

Source: https://habr.com/ru/post/1301834/


All Articles