C
C#17mo 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
Cattywampus17mo ago
you can use the built in .FindAll method
Ice_
Ice_17mo ago
So like GetAllUsers().FindAll(...)? Oooh On the List. Of course Thank you!
Cattywampus
Cattywampus17mo ago
goodluck
Ice_
Ice_17mo ago
Thanks!
Cattywampus
Cattywampus17mo ago
tho Linq would be a better choice here... but it's up to you
ero
ero17mo ago
WhereAsync right
Ice_
Ice_17mo ago
I used Linq. Basically filtered it out whenever I needed it: @foreach (var item in UserList.FindAll(x=>x.Admin==true))
ero
ero17mo ago
that's not linq but if it works that's great
Ice_
Ice_17mo ago
Sorry, lambda
ero
ero17mo 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_17mo ago
Yeah, I'm going to work with naming conventions a bit later. It's just test code right now Thanks!
Angius
Angius17mo 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_17mo 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
Angius17mo 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_17mo 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
Aaron17mo 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
Angius17mo ago
GroupBy() should translate to SQL, IIRC?
Ice_
Ice_17mo ago
I guess. Under the hood. So you mean that there isn't any more efficient way to do things in this case?
Aaron
Aaron17mo ago
^ GroupBy probably if it exists for IQueryable i'd assume it would work fine
Ice_
Ice_17mo 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
ero17mo ago
open a new thread? this seems unrelated
Accord
Accord17mo 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.