0

I have a method that takes an object that can be both int and string, does anyone know a way to make the entity framework to interpret the object !?

I know I can do an if and do the check, but I'm trying to make a simpler way it will use in various parts of the code.

Below is sample code:

public JsonResult LoadForm(object id) {
     if ((form = db.tbSystFormulario.FirstOrDefault(f => f.pk_id.CompareTo(id) == 0)) == null)
     {
          form = db.tbSystFormularioCampo.FirstOrDefault(f => f.tx_nome.CompareTo(id) == 0);
     }

     return Json(LoadForm(form), JsonRequestBehavior.AllowGet); 
}

1 Answer 1

4

You shouldn't be accepting an object in the first place. Have to overloads of the method, one accepting a string, and one accepting an int.

This means that you don't need to check which type the parameter is, it's statically known in the body of each method, and it also prevents the caller from passing in something that's neither a string nor an int. It also means that the caller knows that they need to provide either a string or an int, rather than being unsure what they need to provide.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.