Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

entity framework, how to use object query with a WHERE, AND to retrieve entity record.

Hello,

I am trying to add and AND condition to my entity framework where clause, but the way I am doing it is not working. Can anyone show me how to do this the correct way please? Before adding the AND condition, the code worked fine.

Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & strRecipeId And "it.IngredientItemNumber=" & strIngredientNumber).FirstOrDefault()

The error I get reads as: Conversion from string "it.Recipe_Id=11" to type 'Long' is not valid.

Thank you very much.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of metropia

ASKER

Hi Eric,

I tried your recommendation but I get:

The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation. Near equals expression, line 6, column 44.

for line:
Dim myRecipeStepsHistory = myContext.RefineRecipeStep.Where("it.Recipe_Id=" & strRecipeId & " And it.IngredientItemNumber=" & strIngredientNumber).FirstOrDefault()

Open in new window

Hi metropia;

In order to answer your question we need to know the data type of the following.
Database types for
    Recipe_Id
    IngredientItemNumber
In your code for
    strRecipeId
    strIngredientNumber
got it to work.

Dim myRecipeStepsHistory = myContext.RefineRecipeStep.FirstOrDefault(Function(x) x.Recipe_Id = intRecipeId AndAlso x.IngredientItemNumber = strIngredientNumber)

Open in new window

The original query you were using was doing the query with Query Builder syntax but in your latest post you used method syntax. The reason I was asking for the data types is that string parameters need to be inclosed ' ' and I believe that was the issue but not knowing what was a string and what were other types I was not able to infer from the statement.
Sorry I did not mean not to answer your question.
I would be interested in knowing the alternative solution, following original post.

Database types for
    Recipe_Id   < int
    IngredientItemNumber  < nvarchar(20)
In your code for
    strRecipeId  < string
    strIngredientNumber < string


Thank you much.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial