Getting COUNT and SKIP TAKE in one operation with Linq to Entities

后端 未结 1 1952
终归单人心
终归单人心 2021-01-13 03:33

I have a data call in a Linq to Entities powered data access layer that is designed to make paged calls.

In doing so, I need to select a subset of the data, say 50 r

相关标签:
1条回答
  • 2021-01-13 03:52

    It may be possible, but it probably won't be much faster because of the criteria you're using. Since you're searching for text within a column value, you cannot use an index and thus must do a table scan. You could do a single query to get all records and do the Count and Skip/Take in linq-to-objects:

    var queryResult = DatabaseContext.Table
        .Where(x => !x.IsDeleted)
        .OrderBy(i => i.ID)
        .Where(p => (
                p.PropertyOne.ToLower().Contains(query) ||
                p.PropertyTwo.ToLower().Contains(query) 
                ))
        .ToList();
    
    int count = queryResult.Count();  // now this will be a linq-to-objects query
    
    var returnData = queryResult
        .Skip(start).Take((length))
        .AsEnumerable();
    

    but you'd have to try it to see if it would be any faster.

    0 讨论(0)
提交回复
热议问题