Is linq to sql inefficiently translating this query into the format: "select *"?

I am not very familiar with Linq to SQL, but something that struck me was this:

var articles = 
  (from a in DB.Articles
  where 
  a.ArticleId == ArticleId.Question &&
  a.DeletedAt == null &&
  a.Votes >= minVotes
  orderby a.UpdatedAt descending
  select a).
  Take(maxarticles);

proceeds to the following:

string query = 
  "select top 10 * from articles
  where 
  ArticleId = 1 and 
  DeletedAt is null and 
  Votes >= -5
  order by UpdatedAt desc";

It seems ineffective to me that linq to sql is ready to clear everything using a query like "select *". Is it inefficient?

Why does linq for sql do it this way?

+3
source share
2 answers

If you want to select fewer columns, you will need to use the forecast in your linq query.

var articles = (from a in DB.Articles
    where a.ArticleId == ArticleId.Question 
        && a.DeletedAt == null 
        && a.Votes >= minVotes
    orderby a.UpdatedAt descending
    select new 
    {
       a.ArticleId,
       a.Votes
    })
    .Take(maxarticles);

Doing something like the above translates to SQL, which looks like this:

select top 10 
         ArticleId,
         Votes
from     articles
where    ArticleId = 1 
         and DeletedAt is null
         and Votes >= -5
order by UpdatedAt desc
+4
source

, "SELECT *", , .

linq sql, , "select a"

+4

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


All Articles