LINQ – Enumerable.todictionary only retrieves what it needs?

I am using Enumerable.ToDictionary to create a Dictionary from linq call:

return (from term in dataContext.Terms< br /> where term.Name.StartsWith(text)
select term).ToDictionary(t => t.TermID, t => t.Name);

Whether the call will get every The entire content of each term, or just retrieve the TermID and Name fields from my data provider? In other words, if I write it like this, I will save my database traffic:

return (from term in dataContext.Terms
where term.Name.StartsWith (text)
select new {term.TermID, term.Name }).ToDictionary(t => t.TermID, t => t.Name);

Enumerable.ToDictionary applies to IEnumerable objects. The first part of the statement “(from…choose terms”) is an IQueryable object. Queryable will look at the expression and build the SQL Statement. Then it converts it to IEnumerable to pass to ToDictionary().

In other words, yes, your second version will be more efficient.

I am using Enumerable.ToDictionary to create a Dictionary from linq call:

return (from term in dataContext.Terms
where term.Name.StartsWith(text)
select term).ToDictionary(t => t.TermID, t => t.Name);

Whether the call will get each term Or just retrieve the TermID and Name fields from my data provider? In other words, if I write it like this, I will save my database traffic:

return (from term in dataContext.Terms
where term.Name.StartsWith (text)
select new {term.TermID, term.Name }).ToDictionary(t => t.TermID, t => t.Name);

Enumerable.ToDictionary applies to IEnumerable objects. The first part of the statement “(from…choose terms”) is an IQueryable object. Queryable will look at the expression and build the SQL statement. Then it will convert it to IEnumerable to pass to ToDictionary().

In other words, yes, your second version will be more efficient.

Leave a Comment

Your email address will not be published.