how to fix operator '==' is ambiguous on operands of type ulong and long

后端 未结 3 1316
长情又很酷
长情又很酷 2021-01-27 13:36

I\'m currently building a new command in my Discord Server and i\'m struggling to convert the UserId back to the Users Nickname in my server.

i am getting error message

相关标签:
3条回答
  • 2021-01-27 14:15

    Try casting memberId to a ulong (or UInt64) so the type matches x.Id:

    var memberId = (ulong)reader.GetInt64(0);
    string name = Context.Guild.Users
        .Where(x => x.Id == memberId)
        ...
        ...
    

    Also, you have a few other things to fix as the other answer and comments suggest. ;)

    0 讨论(0)
  • 2021-01-27 14:27

    In the .Where(x => x.Id = memberId), you should use == to compare instead of = (attribution) like you did before.

    Int64 memberId = reader.GetInt64(0);
    string name = Context.Guild.Users
        .Where(x => x.Id == memberId)
        .First()
        .Nickname != null 
            ? Context.Guild.Users.Where(x => x.Id == memberId).First().Nickname 
            : Context.Guild.Users.Where(x => x.Id == memberId).First().Username;
    Int64 votes = reader.GetInt64(2);
    GOTWVote.Add($@"{name} has received {votes} vote(s)");
    

    But you could refactor this code to this (read to comments):

    var memberId = reader.GetInt64(0);
    // search for the user just a single time!
    var user = Context.Guild.Users.First(x => x.Id == memberId);
    
    // apply the rule to define the name string
    string name = @string.IsNullOrEmpty(user.Nickname) ? user.Nickname : user.Username;
    
    var votes = reader.GetInt64(2);
    GOTWVote.Add($@"{name} has received {votes} vote(s)");
    
    0 讨论(0)
  • 2021-01-27 14:35

    The first step is to cast to resolve the ambiguous operator. Next, rearrange your query to get rid of the two extra subqueries:

    Int64 memberId = reader.GetInt64(0);
    var user = Context.Guild.Users
        .Where(x => x.Id == (UInt64)memberId)
        .First();
    
    string name = 
        user.Nickname != null 
            ? user.Nickname 
            : user.Username;
    
    Int64 votes = reader.GetInt64(2);
    GOTWVote.Add($@"{name} has received {votes} vote(s)");
    
    0 讨论(0)
提交回复
热议问题