C
C#•3y ago
Oli

how to search for a value in a list

pretty stupid question but ive tried looking online and i cant seem to find the answer, im fairly sure im using an older .net
36 Replies
Oli
OliOP•3y ago
trying to do it on line 26
pip
pip•3y ago
using System.Linq;
string valueThatImLookingFor = "this";
list.First(p => p.Value/*or whatever the member name is*/ == valueThatImLookingFor);
using System.Linq;
string valueThatImLookingFor = "this";
list.First(p => p.Value/*or whatever the member name is*/ == valueThatImLookingFor);
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Oli
OliOP•3y ago
thanks alot what does p and first refer too
Pobiega
Pobiega•3y ago
p => p.Name == attackerName is a predicate - an anonymous function that takes in an instance of the type the list is generic over, and returns a bool. FirstOrDefault will return the first item that matches the predicate (it returns true), or default(T) if no items matched
Oli
OliOP•3y ago
thanks ill try it again cause i thought p.Name had to be p.name since in the class it was name
Pobiega
Pobiega•3y ago
yeah it has to match the casing of the property in the team_create class you really should be using properties instead of fields for Name etc, and use proper casing (PascalCase for classes, properties, methods - camelCase for local variables) C# doesn't have snake_case as an officially accepted casing read more at https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions
Oli
OliOP•3y ago
im slightly confused i looked at the link and im still pretty lost
Pobiega
Pobiega•3y ago
Well, in coding, we create a lot of things - classes, variables, methods etc right?
Oli
OliOP•3y ago
yep
Pobiega
Pobiega•3y ago
How you name them is important.
Oli
OliOP•3y ago
ohh i get what you mean
Pobiega
Pobiega•3y ago
C# has a "naming convention" which is kind of a standard you should follow
Oli
OliOP•3y ago
i see
Pobiega
Pobiega•3y ago
so other people can easily read and understand your code
Oli
OliOP•3y ago
makes sense so just capitalise the first letter and stuff
Pobiega
Pobiega•3y ago
Thats the basics, yeah so team_create would become TeamCreate snake_case -> PascalCase
Oli
OliOP•3y ago
I see oops
Pobiega
Pobiega•3y ago
public string name; would become public string Name { get; set; } that turns it from a public field to a public property, which might seem pointless now but become important later 🙂
Oli
OliOP•3y ago
alr thanks where would i put that if that makers sense
Pobiega
Pobiega•3y ago
You'd just replace your existing field name with that
Oli
OliOP•3y ago
ohh i was looking in the wrong place
Oli
OliOP•3y ago
so like this
Pobiega
Pobiega•3y ago
Yep! That looks good
Oli
OliOP•3y ago
one last thing, what should i write in catch so that if the value isnt found in the list it goes back to try or do i use something else
Oli
OliOP•3y ago
@Pobiega i tried looking online i found stuff like break but i dont think that will help
Pobiega
Pobiega•3y ago
you'd need a loop for that try/catch is "do this, if an exception occurs inside the try, jump to catch"
Oli
OliOP•3y ago
ah i see i was trying to make it too complicated lol
Oli
OliOP•3y ago
sorry to keep asking but how would i convert it to a boolean
Oli
OliOP•3y ago
wait nvm i cant convert a search into a boolean
Pobiega
Pobiega•3y ago
you just wanna know if there was a match or not? Team1.Any(x => x.Name == P1Check) also, rename your Team1 to team1 :p its a parameter, they should be camelCased same with Check, P1Check etc anything declared inside the method is camelCase
Oli
OliOP•3y ago
checkP1 then
Pobiega
Pobiega•3y ago
sounds good but yeah, First (and FirstOrDefault) is for finding a match if you dont want the match returned, just wanna know if it exists, use .Any
Oli
OliOP•3y ago
thanks
pip
pip•3y ago
i tend to use firstordefault, then checking if the value is null as my scuffed "exists" check. is Any just better by default from a functional / readability standpoint?
Pobiega
Pobiega•3y ago
Shorter, returns a bool And in some cases, like with SQL, it can optimize the query
Want results from more Discord servers?
Add your server