小编mor*_*sol的帖子

关于在存储库模式中使用构造函数的困惑

我是C#的新手,我已经了解了构造函数,而且我了解到我们使用它们的原因是我们可以在构造时分配任何我们想要的东西,但是我很难理解的是,我有一个IRepository学生,例如:

    public  interface IStudentRepository
    {

        object getAll();
    }
Run Code Online (Sandbox Code Playgroud)

我在StudentRepository中实现了它:

     public class StudentRepository:IStudentRepository
    {
       public testDBEntities DB = new testDBEntities();
        public object getAll()
        {
            var reslt = (from s in DB.Students
                     select new StudentViewModel
                     {
                         Name = s.Name,
                         Id=s.Id
                     });

            return reslt;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的homeController中,我可以执行以下操作:

    IStudentRepository _repository=new StudentRepository();   
    public JsonResult Index()
    {
       var m= _repository.getAll();
       return Json(m,JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

为什么我应该有一个构造函数呢?

       IStudentRepository _repository=new StudentRepository();
    public HomeController(StudentRepository _repository)
    {
        this._repository = _repository;
    }
    public JsonResult Index()
    {
       var m= _repository.getAll();
       return Json(m,JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

抱歉,如果我的问题很简单,但是我找不到正确的答案,请先感谢

c# model-view-controller

3
推荐指数
1
解决办法
745
查看次数

如何动态地将参数传递给LINQ where condition

我是LINQ的新手,我需要在同一个LINQ查询上传递不同的条件,如:

  var rslt = (from t in cr.faultStat              
             where(parameter)
             select new{

             name=t.Name,
             Fname=t.fname

             });
Run Code Online (Sandbox Code Playgroud)

在where中看到的参数,是我想发送条件动态的部分,但我不知道如何,因为在哪里接受布尔值,但我想发送类似的东西:

 string parameter="date>"2018-10-10" && id="123"
Run Code Online (Sandbox Code Playgroud)

一般我应该如何动态地向linq发送参数

c# linq

0
推荐指数
1
解决办法
543
查看次数

标签 统计

c# ×2

linq ×1

model-view-controller ×1