本文介绍了LINQ中的强制转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我一直在写一本关于实体框架的自学书,作为一个老学校的开发人员,我手工编写了所有的课程SQL中的数据类和存储过程。 所以我给自己买了一本书,不太方便为C#编写,而从C#转换为VB是显然不是很棘手,vb错过了c#所具有的难以捉摸的var数据类型,并且似乎使用了很多。 无论如何我的问题附带以下代码部分。 昏暗的上下文作为新的Rewards2Entities Dim ThisCustomer As Customer =(来自cust in context。客户选择cust)。第一个 Dim RecSelect As New frmSelection For This ThisPurchase As Purchase in ThisCustomer.Purchases RecSelect.lstPurchases.Items.Add(ThisPurchase.PurchaseDate) Next If RecSelect.ShowDialog = Windows.Forms.DialogResult.Cancel然后退出Sub 结束如果 Dim UpdatePurchase As Purchase =购买时来自ThisCustomer.Purchases _ 凡购买.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString)_ 选择购买 Dim ChangeData As new frmData ChangeData.txtID.Text = UpdatePurchase.Id.ToString ChangeData.txtAmount.Text = UpdatePurchase.Amount.ToString ChangeData.dtpPurchaseDate.Value = UpdatePurchase。 PurchaseDate 如果ChangeData.ShowDialog = Windows.Forms.DialogResult.Cancel则退出Sub 结束如果 UpdatePurchase.Amount = Convert.ToDecimal( ChangeData.txtAmount.Text) UpdatePurchase.PurchaseDate = Convert.ToDateTime(ChangeData.dtpPurchaseDate.Value) context.SaveChanges() MessageBox.Show(记录更新) 我的应用程序在线上无效强制转换时抛出异常 Dim UpdatePurchase 作为购买=来自购买在 ThisCustomer.Purchases _ Where buy.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString)_ 选择购买 例外如下, {无法投射'WhereSelectEnumerableIterator`2类型的对象[TestTestModelFirst。购买,TestTestModelFirst.Purchase]'键入'TestTestModelFirst.Purchase'。} 我已经做了一些谷歌搜索试图解决但它只是想玩。任何人都可以建议解决这个问题吗?我不是特别想要学习C#因为这种烦恼。 非常感谢提前 菲尔。 ps此代码的c#版本可以正常工作。解决方案 LINQ查询返回查询中项目的枚举值。直到你开始通过集合,你才会得到一个项目。要返回强类型项,您应该执行以下操作: Dim UpdatePurchase 作为购买=(来自购买的在 ThisCustomer.Purchases _ 其中purchase.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString)_ 选择购买).FirstOrDefault 然后,看看你得到的错误,我们可以确定LINQ查询返回一个Enumerable(WhereSelectEnumerableIterator`2 [TestTestModelFirst.Purchase,TestTestModelFirst.Purchase]表示 Enumerable(of Purchase)。您不能将枚举器强制转换为类型,您必须实际从枚举数中获取第一个值,上面的代码就是这样做的。 加法ally, var 不是类型,它是类型的省略,所以你也可以写: Dim UpdatePurchases =来自购买的在 ThisCustomer.Purchases _ where purchase.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString)_ 选择购买 这与在C#中使用 var 关键字相同。 Var基本上告诉编译器通过equals右侧的值推断UpdatePurchases的数据类型,在这种情况下UpdatePurchases将是 Enumerable(Of Purchase) 现在可以使用ForEach进行枚举,例如: 对于 每个 purchaseItem as 购买 in UpdatePurchases ' 在buyItem上进行处理 下一步 Hi Guys,I have been working through a book teaching myself a little about the Entity Framework, as an old school developer I coded all my classes by hand, with data classes, and stored procedures in SQL.So I bought myself a book, which not very handily is written for C# and whilst converting from C# to VB is apparently not to tricky, vb is missing the elusive var data type that c# has and seems to use an awful lot.Anyway my issue comes with the following section of code.Dim context As New Rewards2EntitiesDim ThisCustomer As Customer = (From cust In context.Customers Select cust).FirstDim RecSelect As New frmSelectionFor Each ThisPurchase As Purchase In ThisCustomer.Purchases RecSelect.lstPurchases.Items.Add(ThisPurchase.PurchaseDate)NextIf RecSelect.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit SubEnd IfDim UpdatePurchase As Purchase = From purchased In ThisCustomer.Purchases _ Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _ Select purchasedDim ChangeData As New frmDataChangeData.txtID.Text = UpdatePurchase.Id.ToStringChangeData.txtAmount.Text = UpdatePurchase.Amount.ToStringChangeData.dtpPurchaseDate.Value = UpdatePurchase.PurchaseDateIf ChangeData.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit SubEnd IfUpdatePurchase.Amount = Convert.ToDecimal(ChangeData.txtAmount.Text)UpdatePurchase.PurchaseDate = Convert.ToDateTime(ChangeData.dtpPurchaseDate.Value)context.SaveChanges()MessageBox.Show("Record Updated")My application throws an exception on invalid cast on line Dim UpdatePurchase As Purchase = From purchased In ThisCustomer.Purchases _ Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _ Select purchasedException as follows,{"Unable to cast object of type 'WhereSelectEnumerableIterator`2[TestTestModelFirst.Purchase,TestTestModelFirst.Purchase]' to type 'TestTestModelFirst.Purchase'."}I have done some googling to try and resolve but it just want to play. can anyone advise of a resolution to this? I don't particularly want to have to learn C# because of this sort of annoyance.Many Thanks in advancePhil.p.s. the c# version of this code works. 解决方案 LINQ queries return enumerable's for the items in the query. Its not until you start going through the collection that you will get an item. In order to return a strongly typed item, you should do something like this:Dim UpdatePurchase As Purchase = (From purchased In ThisCustomer.Purchases _ Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _ Select purchased).FirstOrDefaultAnd, looking at the error you got, we can determine that the LINQ query is returning an Enumerable (the WhereSelectEnumerableIterator`2[TestTestModelFirst.Purchase,TestTestModelFirst.Purchase] basically means Enumerable(of Purchase). You can't cast an enumerator to a type, you have to actually get the first value from the enumerator, which the code above does. Additionally, var is not a type, its an ommission of a type, so you could also write:Dim UpdatePurchases = From purchased In ThisCustomer.Purchases _ Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _ Select purchasedWhich is the same as using the var keyword in C#. Var basically tells the compiler to infer the data type of UpdatePurchases by the value on the right side of the equals, in this case UpdatePurchases would be an Enumerable(Of Purchase)Which will now let you enumerate using a ForEach, like:For Each purchaseItem as Purchase in UpdatePurchases 'Do your processing on purchaseItem hereNext 这篇关于LINQ中的强制转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 08:44