0

In the following code, I want to retrieve all those records whose Q_ID equals to the ID. Currently, I Select whole records instead of few. Please help me to solve this problem

    public ActionResult Solution(int ID)
    {
        Answers ans_obj = new Answers();

        List<Ans_Table> dbobj= db.Ans_Table.ToList();

        List<Answers> ansobj = dbobj.Select(x => new Answers
        {
            Answer = x.Answer,
            Q_ID=x.Q_ID,
            U_ID=x.U_ID

        }).ToList();

        return View();
    }

1 Answer 1

1

Have you tried a simple Where()?

public ActionResult Solution(int ID)
{
    List<Ans_Table> dbobj = db.Ans_Table.Where(x => x.Q_ID == ID).ToList();

    List<Answers> ansobj = dbobj.Select(x => new Answers
    {
        Answer = x.Answer,
        Q_ID=x.Q_ID,
        U_ID=x.U_ID
    }).ToList();

    return View(ansobj);
}

Side Note: You need to pass something into your View, or it will not display any data. You should read the basics of Asp.Net MVC

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.