❔ 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
you can use the built in
.FindAll
methodList.FindAll(Predicate) Method (System.Collections.Generic)
Retrieves all the elements that match the conditions defined by the specified predicate.
So like GetAllUsers().FindAll(...)?
Oooh
On the List. Of course
Thank you!
goodluck
Thanks!
tho Linq would be a better choice here... but it's up to you
WhereAsync
rightI used Linq. Basically filtered it out whenever I needed it:
@foreach (var item in UserList.FindAll(x=>x.Admin==true))
that's not linq
but if it works that's great
Sorry, lambda
i would suggest maybe choosing some better variable names
Yeah, I'm going to work with naming conventions a bit later. It's just test code right now
Thanks!
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 queryIt'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?
For example this will select all users that have their shoe size bigger than 32
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?
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
GroupBy()
should translate to SQL, IIRC?I guess. Under the hood. So you mean that there isn't any more efficient way to do things in this case?
^ GroupBy
probably
if it exists for IQueryable i'd assume it would work fine
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 🤔
open a new thread? this seems unrelated
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.