Effective and correct Linq queries from the database

Example linq query from MSDN:

var expensiveInStockProducts =
    from p in products
    where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
    select p;

Does this query ask EVERY column from the database table, or returns some pointer that returns the actual data of the column on request? i.e. If my table has 50 columns and I use only one p.UnitsInStock in my actual code, then I get 50 times more data than I expected?

+3
source share
2 answers

if you are debugging this, then you will probably see each column in the table productsin SELECT ...the query part

if you did select p.UnitsInStock, you will only see the SELECT ...query part .

, , , , , , , :

var expensiveInStockProducts =
    from p in products
    where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
    select new { p.UnitsInStock, p.SomeOtherColumn }
+3

LINQPad, SQL, , , ( ), ,

Anonymous

var expensiveInStockProducts =
from p in products
where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
select new {
           p.ColumnIWant1,
           p.ColumnIWant2
          };

var expensiveInStockProducts =
from p in products
where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
select new MyClass() {
          ColumnIWantAsField1 = p.ColumnIWant1,
          ColumnIWantAsField2 = p.ColumnIWant2
          };
+1

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


All Articles