0

I can't succeed to find the right way to do this:

public Task<List<HWElDocument>> GetHWElDocumentsAsync(int? ProjectID = null)
{
    // Get all Items
    if (ProjectID == null)
        return database.Table<HWElDocument>().ToListAsync();

    // Or all items meeting a condition
    else
    {
        // This line has to be awaited, I think ?!?
        List<HWDocuProj> projList = await database.Table<HWDocuProj>().Where( item => item.ProjID == ProjectID).ToListAsync();
        // So that I can use the result here:
        return database.Table<HWElDocument>().Where(item => projList.Exists(p => p.ProjID == item.ID)).ToListAsync();
    }
}

Because of the await keyword, the compiler ask to transform the function in an async function. But I do so, therefore it refuses that the result is a Task<T'> ... If I change the return type to async List<HWElDocument'>, then this line throws an error:

return database.Table<HWElDocument>().ToListAsync();

What is the correct way to achieve this then?

4

2 回答 2

2

You need to "await" an async function whenever you call it. You can do so by simply putting the "await" keyword at the start:

return await database.Table<HWElDocument>().Where(item => projList.Exists(p => p.ProjID == item.ID)).ToListAsync();

and

return await database.Table<HWElDocument>().ToListAsync();

You also need "async" in your method signature:

public async Task<List<HWElDocument>> GetHWElDocumentsAsync(int? ProjectID = null)

Microsoft has a more detailed guide on asynchronous programming in C#: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

于 2021-09-17T21:58:55.963 回答
2

If I change the return type to async List<HWElDocument'>

You need both an async modifier on the method and a Task return type. So this signature should work:

public async Task<List<HWElDocument>> GetHWElDocumentsAsync(int? ProjectID = null)

The async will tell the compiler to internally use an asynchronous state machine and allows you to use the await keyword inside of the method.

And when you want to return something from an asynchronous method, you will need a Task<T> return type. When using async, the compiler also will make sure to wrap your return value in a task automatically, so you can just return the value directly.

So your returning line should also use await in order to await the task from ToListAsync and then the result will be automatically wrapped in your method’s result task:

public Task<List<HWElDocument>> GetHWElDocumentsAsync(int? ProjectID = null)
{
    // Get all Items
    if (ProjectID == null)
        return await database.Table<HWElDocument>().ToListAsync();

    // Or all items meeting a condition
    else
    {
        List<HWDocuProj> projList = await database.Table<HWDocuProj>()
            .Where( item => item.ProjID == ProjectID)
            .ToListAsync();
        return await database.Table<HWElDocument>()
            .Where(item => projList.Exists(p => p.ProjID == item.ID))
            .ToListAsync();
    }
}
于 2021-09-17T22:05:39.497 回答