I am creating a web application, is it possible to use LINQ queries in the Model ,instead of using it in the Controller?
Thanks
I am creating a web application, is it possible to use LINQ queries in the Model ,instead of using it in the Controller?
Thanks
I would recommend you to use your LINQ queries in repository class.
Then you use your Repository methods inside your controller.
Lets say I have a repository class by the name of "CreateRepository" and this is my Createcontroller:
public class CreateController : Controller
{
CreateRepository CreateRepository = new CreateRepository();
public ActionResult Index()
{
var ListOfAllUserNames = CreateRepository.GetAllUserNames();
// etc etc
return View();
}
And this is my CreateRepository Class:
public class CreateRepository
{
public List<User> GetAllUserNames()
{
return db.User.OrderBy(f => f.Username).ToList();
}
}
This is a example on what I mentioned above.
Yes you can use LINQ queries in the model.
Here is an example :
using System.Linq;
using System.Linq.Expressions;
.
.
.
public class PostRepository
{
DataClassesDataContext db = new DataClassesDataContext();
public IQueryable<Post> FindByParentId(int parentId)
{
return from post in db.Posts
where post.ParentID == parentId
select post;
}