What I would expect to see (Never looked at Discord if that's where the database structure comes from) would be a table for:
I would Expect dbContext.Users to have something like:
UserId | UserName
and then a Guilds table to have something like:
GuildId | GuildName | UserId
and Experiences to look something like:
ExperienceId | UserId
I'll continue to make some assumptions here:
Context.Guild.GetUser(n).ToString();
This to me looks like the an EF Core query which translates into SQL:
select UserId from Guild
Working from that, I see a few potential issues:
First, the Guild method returning only a string or long is weird. Return an object if that's your implementation.
More importantly, you can do this likely in 1 query:
in Sql:
Select g.GuildId, e.Experiece, u.UserId, u.UserName from Guild g
left join Users u on g.UserId = u.UserId
left join Experiences e on u.UserId = e.UserId
where g.GuildId = @myGuildId
order by e.Experience Desc
this would give you back rows like:
1 | 1500 | 10 | BitesSpoons
1 | 1450 | 51 | LostElbows
1 | 1121 | 98 | EarthSkyFire
1 | 990 | 15 | GoldenGoose
What I would do: Create a view class, and map it either with something like AutoMapper from a query, or when you materialize the instance:
Make a class:
public class LeaderView
{
public string UserName {get; set;}
public long UserId {get; set;}
public long GuildId {get; set; }
public int Experience { get; set; }
}
And then a linq to Sql query like:
var leaders = from g in context.Guilds
join u in context.Users on g.UserId = u.UserId
join e in context.Experience on u.UserId = e.UserId
select new LeaderView
{
UserName = u.UserName,
UserId = u.UserId,
GuildId = g.UserId,
Experience = e.Experience
};
leaders = leaders.OrderByDescending(o => o.Experience);
return leaders.ToList();