C
C#•17mo ago
kyeede

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.
public class GuildInformation
{
public ulong GuildID { get; set; }
public string GuildName { get; set; }
public string GuildDescription { get; set; }
public string GuildAvatarUrl { get; set; }
public string GuildBannerUrl { get; set; }
public string GuildSplashUrl { get; set; }
public string GuildDiscoveryUrl { get; set; }
public string GuildVanityUrl { get; set; }
public string GuildCreationDate { get; set; }
public ulong GuildOwnerID { get; set; }
public string GuildOwnerName { get; set; }
public int GuildMfaLevel { get; set; }
public int GuildNsfwLevel { get; set; }
public int GuildVerificationLevel { get; set; }
public ulong GuildRulesChannel { get; set; }
public ulong GuildSafetyChannel { get; set; }
public ulong GuildPublicChannel { get; set; }
public ulong GuildModRuleChannel { get; set; }
public GuildPermissions GuildPermission { get; set; }
}

public class GuildPermissions
{
public List<GuildRolePermissions> GuildRolePermission { get; set; }
public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
}

public class GuildRolePermissions
{
public string GuildRoleName { get; set; }
public ulong GuildRoleID { get; set; }
public int GuildRolePermissionLevel { get; set; }
}

public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
{
var GuildRoleCollection = new List<GuildRolePermissions>();

var GuildInformation = new GuildInformation()
{
GuildID = GuildID,
GuildName = GuildName,
// etc...

GuildPermissions = new()
{
foreach (var GuildRole in Guild.Roles
{
var AddRole = new GuildRolePermission
{
GuildRoleName = GuildRole.Name,
GuildRoleID = GuildRole.Id,
GuildRolePermissionLevel = 0
}
};
GuildRoleCollection.Add(AddRole);
}
};
return GuildInformation;
}
public class GuildInformation
{
public ulong GuildID { get; set; }
public string GuildName { get; set; }
public string GuildDescription { get; set; }
public string GuildAvatarUrl { get; set; }
public string GuildBannerUrl { get; set; }
public string GuildSplashUrl { get; set; }
public string GuildDiscoveryUrl { get; set; }
public string GuildVanityUrl { get; set; }
public string GuildCreationDate { get; set; }
public ulong GuildOwnerID { get; set; }
public string GuildOwnerName { get; set; }
public int GuildMfaLevel { get; set; }
public int GuildNsfwLevel { get; set; }
public int GuildVerificationLevel { get; set; }
public ulong GuildRulesChannel { get; set; }
public ulong GuildSafetyChannel { get; set; }
public ulong GuildPublicChannel { get; set; }
public ulong GuildModRuleChannel { get; set; }
public GuildPermissions GuildPermission { get; set; }
}

public class GuildPermissions
{
public List<GuildRolePermissions> GuildRolePermission { get; set; }
public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
}

public class GuildRolePermissions
{
public string GuildRoleName { get; set; }
public ulong GuildRoleID { get; set; }
public int GuildRolePermissionLevel { get; set; }
}

public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
{
var GuildRoleCollection = new List<GuildRolePermissions>();

var GuildInformation = new GuildInformation()
{
GuildID = GuildID,
GuildName = GuildName,
// etc...

GuildPermissions = new()
{
foreach (var GuildRole in Guild.Roles
{
var AddRole = new GuildRolePermission
{
GuildRoleName = GuildRole.Name,
GuildRoleID = GuildRole.Id,
GuildRolePermissionLevel = 0
}
};
GuildRoleCollection.Add(AddRole);
}
};
return GuildInformation;
}
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
Pobiega
Pobiega•17mo ago
Looks like you just want Select
Jimmacle
Jimmacle•17mo ago
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
kyeede
kyeedeOP•17mo ago
You mean the Guild on the property names?
Jimmacle
Jimmacle•17mo ago
yes for example, you already know it's the guild name because it's a property on the GuildInformation object
kyeede
kyeedeOP•17mo ago
Ah, I thought it would look more clear but I do see what you mean yes!
kyeede
kyeedeOP•17mo ago
To add more to the problem I have, these are the errors I face when doing it the way I do.
Jimmacle
Jimmacle•17mo ago
you have a syntax error somewhere, probably mismatched braces
kyeede
kyeedeOP•17mo ago
That's weird, upon removing the entire foreach method, there are 0 errors.
Angius
Angius•17mo ago
Missing semicolon
kyeede
kyeedeOP•17mo ago
It seems like I might have found something that is causing an issue as well apart from the semicolon
kyeede
kyeedeOP•17mo ago
Issue persists
kyeede
kyeedeOP•17mo ago
Errors
Angius
Angius•17mo ago
Ah, well, you can't have a foreach inside of the object initializer You do want a .Select() instead
kyeede
kyeedeOP•17mo ago
Hm, are you able to provide an example of how I should add the .Select()? I'm not sure where exactly I should place that
Angius
Angius•17mo ago
Here
Angius
Angius•17mo ago
GuildRolePermissions = Guild.Roles.Select(r => new GuildRolePermission {
RoleName = g.Name,
RoleId = g.Id,
RolePermissionLevel = 0
}
GuildRolePermissions = Guild.Roles.Select(r => new GuildRolePermission {
RoleName = g.Name,
RoleId = g.Id,
RolePermissionLevel = 0
}
kyeede
kyeedeOP•17mo ago
Ugh, I need to learn LINQ a little more 😭
Pobiega
Pobiega•17mo ago
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
Angius
Angius•17mo ago
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".
Pobiega
Pobiega•17mo ago
so r is your Role from Guild.Roles
kyeede
kyeedeOP•17mo ago
yes
Angius
Angius•17mo ago
Ah, drat, you're right A typo ig
kyeede
kyeedeOP•17mo ago
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
GuildRolePermissions = Guild.Roles.Select(r => new GuildRolePermission
{

}
GuildRolePermissions = Guild.Roles.Select(r => new GuildRolePermission
{

}
Jimmacle
Jimmacle•17mo ago
why do you think it says this?
kyeede
kyeedeOP•17mo ago
A parenthesis is missing but when I close the .Select method, then new GuildRolePermission is no longer recognized. Fixed. The full code would be
RolePermission = Guild.Roles.Select(r => new GuildRolePermissions
{
RoleName = r.Name,
RoleID = r.Id,
RolePermissionLevel = 0
}).ToList()
RolePermission = Guild.Roles.Select(r => new GuildRolePermissions
{
RoleName = r.Name,
RoleID = r.Id,
RolePermissionLevel = 0
}).ToList()
I appreciate you guys for helping me with this! <:KBBQ_slippyPeepoHugged:841064487510474802>
Want results from more Discord servers?
Add your server