简体   繁体   中英

How to return a list of data from my database?

I'm trying to return the users details for a profile but not too sure how. This is what I have so far but not sure if it's even close.

public static async Task<List<UserProf>> ProfileSetUp(string userName, string firstName, string lastName, string profession, string county, string description)
    {
        CurrentPlatform.Init();
        List<UserProf> ls = await Client.GetTable<UserProf>().ToListAsync();
        UserProf u = ls.FirstOrDefault(x => x.Username == userName);
        UserProf f = ls.FirstOrDefault(x => x.Firstname == firstName);
        UserProf l = ls.FirstOrDefault(x => x.Lastname == lastName );
        UserProf p = ls.FirstOrDefault(x => x.Profession == profession);
        UserProf c = ls.FirstOrDefault(x => x.County == county);
        UserProf d = ls.FirstOrDefault(x => x.Description == description);

        List<string> profileList = new List<string>
        {
            userName,
            firstName,
            lastName,
            profession,
            county,
            description
        };
        return profileList;
    }

It's giving me an error:

"Cannot implicitly convert type System.Collection.Generic.List<string> to System.Collection.Generic.List<AppName.UserProf> "

Tried a few solutions to fix but it fixs one error and gives another:

"the return type of an async method must be void Task or Task"

I think what you really want is just a single user based on username and then you can compare that in the calling code to see if the properties are the same or not.

public static Task<UserProf> ProfileSetUp(string userName)
{
    CurrentPlatform.Init();
    return Client.GetTable<UserProf>().SingleOrDefaultAsync(x => x.Username == userName);
}

I think you are trying to filter the database and return a list of matches based on the input.

public static async Task<List<UserProf>> ProfileSetUp(string userName, string firstName, string lastName, string profession, string county, string description)
{
    CurrentPlatform.Init();
    return await Client.GetTable<UserProf>()
        .Where(x => x.Username == userName
                && x.Firstname == firstName
                && x.Lastname == lastName
                && x.Profession == profession
                && x.County == county
                && x.Description == description)
        .ToListAsync();
}

If you want to match where any of the conditions is met then replace all && with || for OR.


There is also no need for async/await as you do not need to wait for the results inside the method. Here is the second variation without that and using OR for any match.

public static Task<List<UserProf>> ProfileSetUp(string userName, string firstName, string lastName, string profession, string county, string description)
{
    CurrentPlatform.Init();
    return Client.GetTable<UserProf>()
        .Where(x => x.Username == userName
                || x.Firstname == firstName
                || x.Lastname == lastName
                || x.Profession == profession
                || x.County == county
                || x.Description == description)
        .ToListAsync();
}

Your problems are:

  1. You aren't returning the right type. You didn't give the full error message (you edited it as I wrote this). It's saying a List<string> doesn't implicitly convert to a List<UserProf> .
  2. You aren't using your variables. You're populating a return value based on the parameters you passed to the method, not the return values of your lambda expressions.

This should fix your problem:

        List<UserProf> profileList = new List<UserProf>
        {
            u,
            f,
            l,
            p,
            c,
            d
        };
        return profileList;

I don't think you'll get what you want, though. I think what you really want is to return one user profile with each of these string values, so you may need to change the function signature to return one single UserProf , create the object in your function, then return the object. You can do that in one lambda:

 UserProf u = ls.FirstOrDefault(x => 
      x.Username == username 
      && x.Firstname == firstname
      && x.Lastname == lastname 
      && x.Profession == profession 
      && x.County == county 
      && x.Description == description);

Also, use proper naming conventions. Compound words like "username" for variables should be "userName" and properties like "x.Firstname" should be "x.FirstName" in the object definition. VS 2017 will call that out as a warning if you don't camel or pascal case your compound words.

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