Last post Jun 05, 2018 07:20 AM by Brando ZWZ
Jun 03, 2018 01:21 AM|slimbunny|LINK
Jun 03, 2018 03:03 AM|DA924|LINK
You can use a Linq projection. The order by would be done as normal.
var results = (from a in db.TableA where a.ID > 2 and a.ID < 100 orderby a.ID
select new // look at Linq projection tutorial
You project using a custom type/class/object which are read/write objects after they have been created.
You can use an Anonymous types, which are read only objects after they have been created.
The data in either case is going to be returned as a results, a collection. Notice the naming convention of 'results' a plural -- more than one.
int count = results.count();
var ordered = results.Orderby(a => a.ID); // you can do that too after you get the results.
Jun 05, 2018 07:20 AM|Brando ZWZ|LINK
Is it possible to make one call to the database and
get fields with row count?
As far as I know, we couldn't directly get the selected count from Linq with orderby.
I suggest you could firstly get the list of model by using the Linq.
Then we could use List.Count method to get the result's count.
It will just make one call to database.
More details, you could refer to below codes sample:
ApplicationDbContext d1 = new ApplicationDbContext();
d1.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
var result =
(from user in d1.Users
UserId = user.Id,
Username = user.UserName
var count = result.Count;