0

I'm using a scaffold-ed index function to get all the data from the database.

// Scaffold-ed index():
public ActionResult Index()
{
    return View(db.Objects.ToList());
}

// Database tables:
[ObjectID] [Name]
1          Tree
2          Plant
3          Flower
4          Tree

Instead of all the objects, I only what the objects with a certain name (for example: tree). What is the best solution for this and should this be done with methods like db.Objects.Find(), Where(),.. or with a custom query?

5
  • 1
    Your question is unclear, and it looks more Linq and/or ORM related rather than an MVC question. Commented Jan 9, 2015 at 14:19
  • You can do that with LINQ, if you have a more complex query you can use a SP for that. Please, be more specific Commented Jan 9, 2015 at 14:20
  • Do a google search for LINQ tutorial Commented Jan 9, 2015 at 14:21
  • It is a simple query, so can you give an example how to do this with LINQ? Commented Jan 9, 2015 at 14:21
  • 1
    db.Objects.Where(w=> w.Name=="SomeName").ToList() Commented Jan 9, 2015 at 14:23

1 Answer 1

2

Linq is good for this the code needed would be

db.Objects.Where(o => o.Name == "Tree").ToList();
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.