Linq-to-sql count

I need to do an element count in a combined result set where the condition is true. So I have the expression "from join where where". This expression must end with select or groupby. I really don't need the column data and the number, so it’s faster not to choose:

count = (from e in dc.entries select new {}).Count();

I have 2 questions:

  • Is there a faster way to do this in terms of db loading?
  • I have to duplicate my entire copy of the request. Is there a way to structure my query where I can have one place for both values ​​and to get a list with all fields?

Thank.

Pay special attention:

  • A query is a join, not a simple table, so I have to use the select statement.
  • I will need two different request bodies, because I do not need to load all the actual fields for the count, but for the list.

I assume that when I use a select query, it is populated with data when I use query.Count vs Table.Count. We look forward to those who understand what I am asking about, about possible ways to do this, and about a detailed knowledge of what is actually happening. I need to pull out a magazine to study it deeper.

+3
source share
2 answers

Queryable.Count

The behavior of the query that occurs as a result of executing the expression tree is that the call to Count (IQueryable) depends on the implementation of the type of the source parameter. the expected behavior is that it counts the number of elements in the source.

, LinqToSql LinqToEntities, Queryable.Count() . . sql .


, select, , query.Count vs Table.Count

. sql .


. ,

, , .

, ... , IQueryable, .Count() .ToList();


, , , .

. , , .

, .

: , .

+6
//you can put a where condition here    
var queryEntries = from e in dc.entries select e;
//Get count 
queryEntries.Count();
//Loop through Entries, so you basically returned all entries
foreach(entry en in queryEntries)
{}
+1

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


All Articles