HOW TO USE OBJECT QUERY WITH A WHERE, AND TO RETRIEVE ENTITY RECORD, ENTITY FRAMEWORK

Pooja Malohtra

Total Post:47

Points:331
Posted by  Pooja Malohtra
 773  View(s)
Ratings:
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.

  1. Goti Bandu

    Post:119

    Points:835
    Re: How to use object query with a WHERE, AND to retrieve entity record, entity framework

    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 =

     

    myContext.RefineRecipeStep

             .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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!