Pro EP 30 : Different ways to write LINQ Query
We have three ways to query data using LINQ as described below
1) LINQ Query Syntax
2) LINQ Method Syntax
𝐋𝐈𝐍𝐐 𝐐𝐮𝐞𝐫𝐲 𝐒𝐲𝐧𝐭𝐚𝐱 : In this method we use SQL query similar style to retrieve data by using these keywords from/select/where.
In first line we define our data source followed by the conditions and selection on next lines.
This can be used while working with complex queries that involved multiple tables.
𝐋𝐈𝐍𝐐 𝐌𝐞𝐭𝐡𝐨𝐝 𝐒𝐲𝐧𝐭𝐚𝐱 : This is functional syntax and most widely used , you can check the second example that is using this approach.
It chains methods to work at the end , it can be helpful while working with simple queries as described in example but that not the rule we can use it for complex queries as well it works fine.
𝐁𝐨𝐭𝐭𝐨𝐦 𝐋𝐢𝐧𝐞 : One works on function pattern and other on SQL Query style , both are powerful , its up to you which one you are going to choose , I personally like Method approach because Query approach looks like verbose to me
If you want to help the author in growing
- Subscribe my Weekly .NET Newsletter of C#/.NET with 800+ Software Engineers.
- Become Patron to get access to 90+ Compiled Questions and Answers at one place.
- Download my eBook at Gum road that contains 30+ .NET Tips.