Home > DeveloperSection > Forums > How to use object query with a WHERE, AND to retrieve entity record, entity framework
Pooja Malohtra
Pooja Malohtra

Total Post:47

Posted on    January-19-2015 10:25 PM

 ASP.Net C#  .NET  VB.Net  Entity Framework 

 1 Reply(s)
 488  View(s)
Rate this:

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.

Goti Bandu

Total Post:119

Posted on    January-19-2015 11:44 PM

I would try to use the built in linq if you can. It will check your values at compile time for you and you won't get nasty surprises at run time. You can use magic-strings of course but why not use one of the best features of of an ORM? Imagine updating a tables field type, the compiler will automatically let you know every place you need to update your code. Instead of having forgetting about one and then having it crash here and there.

If you're querying a single table, often it's easier to put the 'wheres' inside the FirstOrDefault();

Dim myRecipeStepsHistory =



         .FirstOrDefault(x => x.Recipe_Id.Equals(strRecipeId) &&

                              x.IngredientItemNumber == strIngredientNumber);


if(myRecipeStepHistory != null)




You may have to cast your values so it matches the correct data type for Recipe_Id and IngredientItemNumber.

Don't want to miss updates? Please click the below button!

Follow MindStick