Foreach loop inside List<T> class
Okay so I'm actually quite confused on this. I have a collection of roles that I'd like to store inside a JSON file. My goal is to obtain every role's information within a guild by first obtaining the RoleCollection and then initiating a foreach loop to add it into a class list so later on I can serialize that information into a JSON file.
Is something like this possible? I'd like to do this way so upon the system detecting a new guild, it stores basic guild information and each role (to set up a permission-like system for commands)
25 Replies
Looks like you just want
Select
i would also recommend against prefixing all your property names - you do you of course but to me it adds a lot of noise without any benefit
You mean the Guild on the property names?
yes
for example, you already know it's the guild name because it's a property on the GuildInformation object
Ah, I thought it would look more clear but I do see what you mean yes!
To add more to the problem I have, these are the errors I face when doing it the way I do.
you have a syntax error somewhere, probably mismatched braces
That's weird, upon removing the entire foreach method, there are 0 errors.
Missing semicolon
It seems like I might have found something that is causing an issue as well apart from the semicolon
Issue persists
Errors
Ah, well, you can't have a foreach inside of the object initializer
You do want a
.Select()
insteadHm, are you able to provide an example of how I should add the
.Select()
?
I'm not sure where exactly I should place thatHere
Ugh, I need to learn LINQ a little more ðŸ˜
Close. The lambda function you give takes in an item from the collection you call select on, and it should return an item in the resulting collection
LINQ is love, LINQ is life
I was only nine years old. I loved LINQ so much, I had all the merchandise and movies. I'd pray to LINQ every night before I go to bed, thanking for the life I've been given. "LINQ is love", I would say, "LINQ is life".
so
r
is your Role from Guild.Roles
yes
Ah, drat, you're right
A typo ig
It does say that
) expected
which once I apply either at the end of the bracket or at (r => new GuildRolePermission)
doesn't recognize it anymore
why do you think it says this?
A parenthesis is missing but when I close the
.Select
method, then new GuildRolePermission
is no longer recognized.
Fixed. The full code would be
I appreciate you guys for helping me with this! <:KBBQ_slippyPeepoHugged:841064487510474802>