I'm working with vb.net and entity framework.

In my form , I have 2 comboboxes : Article and price. ( For both comboboxes , .Selectedvalue is an integer )

If I use this expression :

Dim gj As IEnumerable(Of Myobject)
gj = (From t In context.myobjects Where t.art = Article.SelectedValue And t.prc = price.SelectedValue
Select t).ToList

an error is produced :

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dllAdditional information: LINQ to Entities does not recognize the method 'System.Object CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.

If I use this code :

Dim gj As IEnumerable(Of Myobject)
gj = (From t In context.myobjects Where t.art = Article.SelectedValue.ToString And t.prc = price.SelectedValue.ToString
Select t).ToList

everything is OK.

The problem is that i'm not sure that using the ToString method could produce a correct result everytime because t.art and t.prc are integers.

Can you give me an explanation , and what should i do ?

Thank you !

Assuming Article and price are some sort of list control, generally the items are stored as objects. In order to use them you must cast them to the appropriate type.

Your choices are simple, either use the ToString method or cast the SelectedValue's to integers(DirectCast method will do). The one you choose will depend on how your class is set up. With integers the ToString method is very safe. It will produce consistent results.

( First , to make it clear , Article and Price are Comboboxes )
I try using Convert.toint32 ,
Dim gj As IEnumerable(Of Myobject)
gj = (From t In context.myobjects Where t.art = Convert.Int32(Article.SelectedValue) And t.prc = Convert.Toint32(price.SelectedValue)
Select t).ToList
but I get an error :
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression.

After I try to use convert.Toint32 but keeping the Tostring method.
Dim gj As IEnumerable(Of Myobject)
gj = (From t In context.myobjects Where t.art = Convert.Int32(Article.SelectedValue.Tostring) And t.prc = Convert.Toint32(price.SelectedValue.Tostring)
Select t).ToList
Again I get an error :
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

The method only with ToString works ok in this case , but i want to know if this method works in other cases too.
for example what if i want to compare inside the query values of Decimal type with the .Tostring , or single... etc.

So i want to know if the method only with the ToString is correct according to VB.net and Entity Framework ?
Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.