C
C#2y ago
barcode

✅ EFCore LINQ methods

I have this function
public async Task<IReadOnlyList<Review>> GetByCenterId(uint id, uint page, uint count)
{
return await _dbContext.Reviews
.Where(x => x.CenterId == id)
.OrderBy(x => x.CreatedAt)
.Skip((int)(page * count))
.Take((int)count)
.ToListAsync();
}
public async Task<IReadOnlyList<Review>> GetByCenterId(uint id, uint page, uint count)
{
return await _dbContext.Reviews
.Where(x => x.CenterId == id)
.OrderBy(x => x.CreatedAt)
.Skip((int)(page * count))
.Take((int)count)
.ToListAsync();
}
and naturally pages and count cannot be negative so I use uint, however I must cast it as no Skip or Take method exists for uint, is this the way it should be done or should I use int as parameters?
7 Replies
Angius
Angius2y ago
Either approach works, honestly In one project, I just use ints but with a Math.Max(0, page) In another, ints directly Using uints and casting is also valid
JakenVeina
JakenVeina2y ago
I mean, bottom line is that Linq doesn't support datasets larger than in.MaxValue records if you want to write your own layers to indicate support for larger datasets, you CAN but it's fake support
Angius
Angius2y ago
That too, uint can exceed int.MaxValue
JakenVeina
JakenVeina2y ago
in theory you COULD write your own .Skip() and .Take() methods that accept uint and write extensions for EF to translate them into SQL if you needed that support in the future IMO, I would not code your business layers to pretend to have support for something that the underlying data layer doesn't other than that, Z's right, it doesn't really matter
I mean, bottom line is that Linq doesn't support datasets larger than int.MaxValue records
for clarity, this is because basically EVERYTHING in Linq assumes int for indexes and sizes .NET in general, for that matter Array.Length is int List<T>.Count is int
Angius
Angius2y ago
Even list and array indexes are int, despite negative indexes not being a thing
JakenVeina
JakenVeina2y ago
it's a compatibility decision, really uint is not CLS-compliant for whatever that's worth
barcode
barcodeOP2y ago
ty will just swtich to int

Did you find this page helpful?