views:

64

answers:

2

I'm not super familiar with Linq to SQL yet but something that strikes me is that 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);

gets translated to this:

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

It strikes me as inefficient that linq to sql is willing to vacuum up everything using a "select *" type of query. Isn't this inefficient?

Why does linq to sql do it this way?

+3  A: 

The ideia behind not using "SELECT *" is to avoid bringing unecessary columns.

In your case, you are specifically asking linq to sql to bring all columns when you specified "select a"

Alfred Myers
+1  A: 

If you want to select fewer columns, you'll have to use a projection 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 would translate into SQL that looks like this...

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