简体   繁体   中英

How to convert system.collections.generic.list<className> to string

Trying to convert database value to string I'm having trouble because its not convert the value but system.collections.generic.list

try
{
    var finalColor = await App.Database.getColorUser(_username);
    await DisplayAlert("COLOR", finalColor.ToString(), "OK");
}
catch (Exception ex)
{
    await DisplayAlert("ERROR", ex.Message, "Ok");
}

Database table :

public Task<List<UserColor>> getColorUser(string userNameColor)
{
    var query = database.QueryAsync<UserColor>("Select ColorUser From UserColor Where UserName='"+userNameColor+"'");
    return query;
}

FirstOrDefault will get the first in the list or null if enmpty... assuming there is only going to be logical one result

public async Task<UserColor> getColorUser(string userNameColor)
    {
        var list = await database
                       .QueryAsync<UserColor>("Select ColorUser From UserColor Where UserName='"+userNameColor+"'")
                       .ConfigureAwait( false );
        return list.FirstOrDefault();       
    }

Resources

Enumerable.FirstOrDefault Method (IEnumerable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM