Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 Rewards2Entities
Dim ThisCustomer As Customer = (From cust In context.Customers Select cust).First
Dim RecSelect As New frmSelection
For Each ThisPurchase As Purchase In ThisCustomer.Purchases
    RecSelect.lstPurchases.Items.Add(ThisPurchase.PurchaseDate)
Next
If RecSelect.ShowDialog = Windows.Forms.DialogResult.Cancel Then
    Exit Sub
End If

Dim UpdatePurchase As Purchase = From purchased In ThisCustomer.Purchases _
                                 Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
                                 Select purchased


Dim ChangeData As New frmData
ChangeData.txtID.Text = UpdatePurchase.Id.ToString
ChangeData.txtAmount.Text = UpdatePurchase.Amount.ToString
ChangeData.dtpPurchaseDate.Value = UpdatePurchase.PurchaseDate

If ChangeData.ShowDialog = Windows.Forms.DialogResult.Cancel Then
    Exit Sub
End If

UpdatePurchase.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
VB
Dim UpdatePurchase As Purchase = From purchased In ThisCustomer.Purchases _
                                         Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
                                         Select purchased


Exception 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 advance

Phil.

p.s. the c# version of this code works.
Posted

1 solution

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:

VB
Dim UpdatePurchase As Purchase = (From purchased In ThisCustomer.Purchases _
                                         Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
                                         Select purchased).FirstOrDefault


And, 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:

VB
Dim UpdatePurchases = From purchased In ThisCustomer.Purchases _
                                         Where purchased.PurchaseDate.ToString = Convert.ToDateTime(RecSelect.lstPurchases.SelectedItem.ToString) _
                                         Select purchased


Which 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:

VB
For Each purchaseItem as Purchase in UpdatePurchases
    'Do your processing on purchaseItem here
Next
 
Share this answer
 
v3
Comments
tiggerc 22-Jan-14 11:42am    
Thank you kind sir, you are a star, it has been bugging me all day,
I tried just using dim UpdatePurchase however my ide went nuts with errors.

Never mind, Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900