将SQL查询数据从模型返回到控制器

时间:2018-10-10 04:58:52

标签: asp.net-mvc entity-framework

我正在尝试设计一个sql数据板/仪表板。 我已经使用实体框架6连接到SQL数据库。

我试图通过在Model中进行查询,然后将值传递给Homecontroller来实现这一目标

型号

 public class LaserRepo
{
    public string CmRlsd3D { get; set; }

    public void CMRlsd3Days()
    {
        WorkflowEntities db = new WorkflowEntities();
        int CurrentYear = DateTime.Now.Year;
        DateTime firstday = new DateTime(CurrentYear, 1, 25);

        DateTime prevyear = new DateTime(2017, 10, 1);

        DateTime Yday = DateTime.Today.AddDays(-2);
        string Dayofweek = Yday.DayOfWeek.ToString();
        int day1 = (int)(DateTime.Now.DayOfWeek);
        if (day1 == 1)
        {
            Yday = DateTime.Today.AddDays(-4);
        }

        CmRlsd3D   = (from c in db.OrderDetailCheckHistories
                        join d in db.OrderDetails on c.JobNumber equals d.JobNumber
                        where (c.DateTime > firstday && c.DateTime < Yday)
                        where c.FieldName == "Released" || c.FieldName == "Pre Release"
                        where (c.StockCode.Contains("-CA") && !(c.StockCode.Contains("-CAB"))) ||
                        c.StockCode.Contains("-CM") || c.StockCode.Contains("-LP")
                        select new { c.JobNumber }).ToString();                  


    } 

我想将总数传递给我的控制器/视图

CmRlsd3D 

家庭控制器

public class HomeController : Controller
{    
    public ActionResult Laser()
    {
        LaserRepo Lr = new LaserRepo();
        var Cm3D = Lr.CmRlsd3D.Count();
        return View(Lr);
    }

1 个答案:

答案 0 :(得分:1)

首先,您应该知道Lr.CmRlsd3D.Count();会计算string属性中某些元素的数量,而不是预期的结果集数量。另外,您那里还有许多问题:

a)多个where子句是多余的,可能不会返回期望的结果。将&&运算符用于多个使用逻辑where的{​​{1}}条件。

b)AND使用匿名类型,我认为这是不必要的。 select new { c.JobNumber }就足够了。

c)select c.JobNumber针对LINQ查询可能返回结果集类型的标准名称而不是值。您需要使用ToString()FirstOrDefault()SingleOrDefault()循环迭代来从中返回字符串值。

因此,您应该使用以下设置:

1)在viewmodel类中使用一个整数属性来保存计数。

foreach

2)将方法返回类型更改为public int CmRlsd3D { get; set; } ,并使用int修改查询以返回记录数,如下例所示:

Count()

3)通过方法的结果分配属性。

public int CMRlsd3Days()
{
    WorkflowEntities db = new WorkflowEntities();
    int CurrentYear = DateTime.Now.Year;
    DateTime firstday = new DateTime(CurrentYear, 1, 25);

    DateTime prevyear = new DateTime(2017, 10, 1);

    DateTime Yday = DateTime.Today.AddDays(-2);
    string Dayofweek = Yday.DayOfWeek.ToString();
    int day1 = (int)(DateTime.Now.DayOfWeek);
    if (day1 == 1)
    {
        Yday = DateTime.Today.AddDays(-4);
    }

    var count = (from c in db.OrderDetailCheckHistories
                join d in db.OrderDetails on c.JobNumber equals d.JobNumber
                where (c.DateTime > firstday && c.DateTime < Yday)
                && (c.FieldName == "Released" || c.FieldName == "Pre Release")
                && ((c.StockCode.Contains("-CA") && !(c.StockCode.Contains("-CAB"))) ||
                c.StockCode.Contains("-CM") || c.StockCode.Contains("-LP"))
                select c.JobNumber).Count();               

    return count;
}

4)在视图页面中,使用public ActionResult Laser() { LaserRepo Lr = new LaserRepo(); Lr.CmRlsd3D = CMRlsd3Days(); return View(Lr); } // alternative using property get definition public int CmRlsd3D { get { return CMRlsd3Days(); } } @model LaserRepo进行显示。