C
C#2y ago
Ice_

❔ Filter list with lambda expression?

Hello! Right now I have a "MyList = blahaRepo.GetAllUsers();" which retrieves all users. Instead of creating a separate method for retrieving users with a specific flag or creating a new list and run a foreach and add those with the specific flag to the new list - Can I simply use a lambda expression along with the line above to filter out while filling the first list? Or is there a better way to filter out the data?
23 Replies
Cattywampus
Cattywampus2y ago
you can use the built in .FindAll method
Ice_
Ice_OP2y ago
So like GetAllUsers().FindAll(...)? Oooh On the List. Of course Thank you!
Cattywampus
Cattywampus2y ago
goodluck
Ice_
Ice_OP2y ago
Thanks!
Cattywampus
Cattywampus2y ago
tho Linq would be a better choice here... but it's up to you
ero
ero2y ago
WhereAsync right
Ice_
Ice_OP2y ago
I used Linq. Basically filtered it out whenever I needed it: @foreach (var item in UserList.FindAll(x=>x.Admin==true))
ero
ero2y ago
that's not linq but if it works that's great
Ice_
Ice_OP2y ago
Sorry, lambda
ero
ero2y ago
i would suggest maybe choosing some better variable names
@foreach (var user in UserList.FindAll(u => u.IsAdmin))
@foreach (var user in UserList.FindAll(u => u.IsAdmin))
Ice_
Ice_OP2y ago
Yeah, I'm going to work with naming conventions a bit later. It's just test code right now Thanks!
Angius
Angius2y ago
If you're using EF, this will load all the users and then filter them What you want is to include a .Where() in your EF query
Ice_
Ice_OP2y ago
It's EF. And exactly It's loading everything from a database server. I know it's not effecient loading every user from the list. Can you give me an example of using .Where? And whwre would I use it? When using dbcontext?
Angius
Angius2y ago
var users = await _context.Users
.Where(u => u.ShoeSize > 32)
.ToListAsync();
var users = await _context.Users
.Where(u => u.ShoeSize > 32)
.ToListAsync();
For example this will select all users that have their shoe size bigger than 32
Ice_
Ice_OP2y ago
That really limits the data! The "problem" I am having is that I want to create two tables (html <table>), differenting between between ordinary and admin users. Using a LInq sort of makes me want to create two lists? But that is maybe the best way?
Aaron
Aaron2y ago
FindAll would've created two lists actually, it would've made 3 the original list, a list of normal users, and a list of admin users
Angius
Angius2y ago
GroupBy() should translate to SQL, IIRC?
Ice_
Ice_OP2y ago
I guess. Under the hood. So you mean that there isn't any more efficient way to do things in this case?
Aaron
Aaron2y ago
^ GroupBy probably if it exists for IQueryable i'd assume it would work fine
Ice_
Ice_OP2y ago
Alright! Thanks! I'm having an issue with Blazor and its components. You don't happen to know some stuff about that? <PageTitle> is literally being overwritten by one or more sub-components. I've tried googling the crap out of that but not found anything valuable 🤔
ero
ero2y ago
open a new thread? this seems unrelated
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server