不能使用实体框架6将void赋值给隐式类型的局部变量

本文关键字:类型 局部变量 赋值 void 实体 框架 不能 | 更新日期: 2023-09-27 18:01:18

我使用WCF引用从我的Quote表调用最后一行。现在我在我的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()方法,但它给了我以下错误:

不能将void赋值给隐式类型的局部变量

这是我的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();
    }
}

我见过一些人有同样的问题,但我不完全明白如何在我自己的问题中实现解决方案。如果有人能帮忙,我将不胜感激!:)

不能使用实体框架6将void赋值给隐式类型的局部变量

你想调用你的查询返回的内容,但是环绕该查询的方法没有返回任何内容,因为它的类型是void。

我假设您想返回类型为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()你张贴了错误的方法,这将抛出相同的错误?

如果这个方法也有async版本,它应该看起来像这样:

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;
    }
}