ViewComponent 对我的查询不满意

ViewComponent not happy about my query

我正在努力掌握 ViewComponents。我有一个用于购物车的 ViewComponent class:

public class ShoppingCartViewComponent : ViewComponent
{
    private readonly MyStoreContext _context;

    public ShoppingCartViewComponent(MyStoreContext context)
    {
        _context = context;
    }

    public async Task<IViewComponentResult> InvokeAsync(int Id)
    {
        return View(await GetCartAsync(Id));
    }

    private Task<ViewModelShoppingCart> GetCartAsync(int Id)
    {
        var VMCart = _context.ShoppingCarts
                    .Where(c => c.Id == Id)
                    .Select(cart => new ViewModelShoppingCart
                    {
                        Id = cart.Id,
                        Title = cart.Title,
                        CreateDate = cart.CreateDate,
                        ShoppingCartItems = cart.ShoppingCartItems
                                            .Select(items => new ViewModelShoppingCartItem
                                            {
                                                ProductId = items.ProductId,
                                                ProductTitle = items.Product.Title,
                                                ProductPrice = items.Product.Price,
                                                Quantity = items.Quantity
                                            }).ToList()
                    }).FirstOrDefault();
        return VMCart;
    }
}

这会导致 return VMCart 上的构建错误,并显示来自 VS 的消息:

Cannot implicitly convert type 'System.Linq.IQueryable<MyStore.Models.ViewModels.ViewModelShoppingCart>' to 'System.Threading.Tasks.Task<MyStore.Models.ViewModels.ViewModelShoppingCart>'. An explicit conversion exists (are you missing a cast?)

我做错了什么? 编辑 在我的查询末尾添加了 .FirstOrDefault(),但错误仍然存​​在。

问题是您的函数定义看起来像是在尝试创建一个异步方法,但内部没有任何内容是异步的。将其更改为:

private async Task<ViewModelShoppingCart> GetCartAsync(int Id)
{
    var VMCart = await _context.ShoppingCarts
                .Where(c => c.Id == Id)
                .Select(cart => new ViewModelShoppingCart
                {
                    Id = cart.Id,
                    Title = cart.Title,
                    CreateDate = cart.CreateDate,
                    ShoppingCartItems = cart.ShoppingCartItems
                                        .Select(items => new ViewModelShoppingCartItem
                                        {
                                            ProductId = items.ProductId,
                                            ProductTitle = items.Product.Title,
                                            ProductPrice = items.Product.Price,
                                            Quantity = items.Quantity
                                        }).ToList()
                }).FirstOrDefaultAsync();
    return VMCart;
}

或者让它不异步:

private ViewModelShoppingCart GetCart(int Id)
{
    var VMCart = _context.ShoppingCarts
                .Where(c => c.Id == Id)
                .Select(cart => new ViewModelShoppingCart
                {
                    Id = cart.Id,
                    Title = cart.Title,
                    CreateDate = cart.CreateDate,
                    ShoppingCartItems = cart.ShoppingCartItems
                                        .Select(items => new ViewModelShoppingCartItem
                                        {
                                            ProductId = items.ProductId,
                                            ProductTitle = items.Product.Title,
                                            ProductPrice = items.Product.Price,
                                            Quantity = items.Quantity
                                        }).ToList()
                }).FirstOrDefault();
    return VMCart;
}