wpf 无法使用实体框架 6 将 void 分配给隐式类型的局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31267409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot assign a void to an implicitly-typed local variable using Entity Framework 6
提问by CareTaker22
I am using a WCF refrenceto call the last row from my Quotetable. Now I wrote this method in my WCF application to get the last row, but I do not know it it works or not(I am trying to test it):
我正在使用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();
}
}
In my WPF application I am using the WCF reference and calling on the GetLastQuoteAsync()method, but it is giving me the following error:
在我的 WPF 应用程序中,我正在使用 WCF 引用并调用该GetLastQuoteAsync()方法,但它给了我以下错误:
Cannot assign void to an implicitly-typed local variable
不能将 void 分配给隐式类型的局部变量
And here is the method in my WPF application where I am trying to call the GetLastQuoteAsync()reference.
这是我尝试调用GetLastQuoteAsync()引用的WPF 应用程序中的方法。
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();
}
}
I have seen some people with the same question, but I do not fully understand how to implement the solutions in my own problem. If anyone could help, I would appreciate it allot! :)
我见过一些人提出同样的问题,但我不完全了解如何在我自己的问题中实施解决方案。如果有人可以提供帮助,我将不胜感激!:)
回答by Marco
You want to call what your query is returning, but the method wrapping around that query is returning nothing, because its type is void.
您想调用查询返回的内容,但环绕该查询的方法不返回任何内容,因为它的类型为 void。
I assume you want to return an object of type Quote, so you need to change your method to this:
我假设您想返回一个 type 对象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;
}
}
Also GetLastQuote()is not the same as GetLastQuoteAsync()Have you posted the wrong method, which would throw the same error?
同样GetLastQuote()是不一样的GetLastQuoteAsync()你贴错了方法,它会抛出同样的错误?
If there is also async version of this method it should probably look similar to this:
如果此方法也有异步版本,它可能看起来类似于:
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;
}
}

