无法使用 Entity Framework 将 void 分配给隐式类型的局部变量 6

Cannot assign a void to an implicitly-typed local variable using Entity Framework 6

我正在使用 WCF 引用 调用我的 Quote table 的最后一行。现在我在我的 WCF 应用程序中写了这个方法来获取最后一行,但我不知道它是否有效(我正在尝试测试它):

public void GetLastQuote()
{
    using (TruckDb db = new TruckDb())
    {
        var quote = (from qData in db.Quotes
                     where qData.Id == qData.RepresetativeId
                     orderby qData.Id descending
                     select qData).First();
    }
}

在我的 WPF 应用程序中,我正在使用 WCF 引用并调用 GetLastQuoteAsync() 方法,但它给我以下错误:

Cannot assign void to an implicitly-typed local variable

这是我的 WPF 应用程序中尝试调用 GetLastQuoteAsync() 引用的方法。

private async void wListOfBills_Loaded(object sender, RoutedEventArgs e)
{
    using (TruckServiceClient client = new TruckServiceClient())
    {
        var quote = await client.GetLastQuoteAsync(); // -> This is where the error lies.
        var bills = await client.GetListOfBillsAsync(quote.Item.Id);
        if (bills == null)
        {
            dgFloor.IsEnabled = true;
            return;
        }
        dgFloor.ItemsSource = bills.Select(x => new ListOfBillsView
        {
            Code = x.StockCode,
            Group = x.GroupName,
            Description = x.StockDescription,
            Qty = x.Quantity,
            Length = x.Length,
            Width = x.Width,
            Weight = x.Weight,
            Price_m = x.PricePerMeter,
            Cost = x.Cost,
            Section = x.TruckSection.ToString()
        }).ToList();
    }
}

我看到有人有同样的问题,但我不完全明白如何在我自己的问题中实施解决方案。如果有人可以提供帮助,我将不胜感激! :)

您想调用您的查询 returning,但是围绕该查询的方法 return 什么都没有,因为它的类型是 void。

我假设您想要 return 类型 Quote 的对象,因此您需要将方法更改为:

//change from void to Quote
public Quote GetLastQuote()
{
    using (TruckDb db = new TruckDb())
    {
        var quote = (from qData in db.Quotes
                     where qData.Id == qData.RepresetativeId
                     orderby qData.Id descending
                     select qData).First();
        //new
        return quote;
    }
}

还有GetLastQuote()GetLastQuoteAsync()不一样,你贴错方法了,会报同样的错误吗?

如果此方法也有异步版本,它可能看起来类似于:

public async Task<Quote> GetLastQuote()
{
    using (TruckDb db = new TruckDb())
    {
        var quote = (from qData in db.Quotes
                     where qData.Id == qData.RepresetativeId
                     orderby qData.Id descending
                     select qData).FirstAsync(); /*Note async method here*/
        //new
        return await quote;
    }
}