0

in controller

public ActionResult Index(int id)
{
    string sql = string.Format("SELECT * FROM emp WHERE empno = {0}", 3195);
    var ret = db.Database.SqlQuery<int>(sql).ToList();

     // I want to fetch above query result here...
     // Above query returns more than 1 rows and more than 1 column
     // Like ret.Column1(row0).value

    return RedirectToAction("Index");
}

how can I fetch result in the same function.. i need to perform some calculation here and saved the result in database again.

1 Answer 1

1

you need to do like this. Create a ViewModel:

public class EmployeeVM
{
public int Id {get;set;}
public string EmployeeName {get;set;}
}

action:

public ActionResult Index(int id)
{
    string sql = string.Format("SELECT Id,EmployeeName FROM emp WHERE empno = {0}", 3195);
    var ret = db.Database.SqlQuery<EmployeeVM>(sql).ToList();

     /
    return RedirectToAction("Index");
}

Remember that your model properties name should match the column name of the table, as in my case Id and EmployeeName property are mataching with column name.

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

4 Comments

Thanks, can you also tell me if I use ToArray instead of ToList and How?
you should use list in my opinion of you want to use array simply replace ToList with ToArray
Then how can I get value from an arrey
same way as you get from List

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.