【问题标题】:ASP.NET MVC many to many relationship errorASP.NET MVC 多对多关系错误
【发布时间】:2020-10-13 23:52:06
【问题描述】:

这是我的控制器:

  public ActionResult PPKPIChart()
    {
        return View();
    }
        public ActionResult PPKPI()
    {
        var dt = db.Sales
               .Where(ol => ol.SaleId != null)
               .GroupBy(ol => ol.PPUsers.Where(p => p.Name != null))
               .ToList()
               .Select(g => new object[] {
                       g.Key,
                       g.Count()
                   }); ;
        return Json(dt, JsonRequestBehavior.AllowGet);
    }

这是我的看法:

@section foot {
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
    google.charts.load('current', { packages: ['corechart'] });
    google.charts.setOnLoadCallback(init);

    let dt, opt, cht;

    function init() {
        dt = new google.visualization.DataTable();
        // TODO: Data table columns
        dt.addColumn('string', 'Name');
        dt.addColumn('number', 'KPI');

        let style = { bold: true, italic: false, fontSize: 20, color: 'purple' };

        opt = {
            title: 'Prepress Personnel KPI',
            fontName: 'calibri',
            fontSize: 14,
            titleTextStyle: { fontSize: 20 },
            chartArea: {
                width: '80%',
                height: '70%',
                top: 60,
                left: 80
            },
            // TODO: vAxis, hAxis, legend, animation, orientation
            vAxis: {
                title: 'KPI',
                titleTextStyle: style
            },
            hAxis: {
                title: 'Name',
                titleTextStyle: style
            },
            legend: 'none',
            animation: {
                duration: 500,
                startup: true
            },
            orientation: 'horizontal',
        };

        cht = new google.visualization.ColumnChart($('#chart')[0]);

        $('#reload').click();
    }

    $('#reload').click(function (e) {
        e.preventDefault();

        let url = '/Home/PPKPI';
        let param = {};

        $.getJSON(url, param, function (json) {
            dt.removeRows(0, dt.getNumberOfRows());
            dt.addRows(json);
            cht.draw(dt, opt);
        });
    });

    $('#toggle').click(function (e) {
        e.preventDefault();

        // TODO: Toggle orientation (horizontal <--> vertical)
        opt.orientation = opt.orientation == 'horizontal' ?
                          'vertical' : 'horizontal';
        [opt.vAxis, opt.hAxis] = [opt.hAxis, opt.vAxis];

        cht.draw(dt, opt);
    });
</script>

这是我的实体框架,具有多对多关系

我想显示条形图,但由于多对多的关系,我不能这样做,尤其是在

GroupBy(ol => ol.PPUsers.Where(p => p.Name != null)) -- > i guess this is the issues behind it.. my query in this line is not working 

基本上,关系如下所示:

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework linq


    【解决方案1】:
     `public ActionResult TotalJobAssignedData()<br/>
        {
            var dt = db.PPUsers
                   .ToList()
                   .Select(g => new object[] {
                           g.Name, 
                           g.Sales.Count()
                   });
            return Json(dt, JsonRequestBehavior.AllowGet);
        }
    

    【讨论】:

      【解决方案2】:

      似乎相关的PPUsers 实体无法加载到Sales 实体中。

      您可以按照Eagerly Loading的方法:

      急切加载是查询一种类型的实体的过程 还加载相关实体作为查询的一部分。急切加载是 使用 Include 方法实现。

      为此,您可以使用Include 加载相关实体,如下所示:

       var dt = db.Sales
                 .Include(s => s.PPUsers)
                 .Where(ol => ol.SaleId != null)
                 .GroupBy(ol => ol.PPUsers.Where(p => p.Name != null))
                 .ToList()
                 .Select(g => new object[] {
                         g.Key,
                         g.Count()
                     }); 
      

      Entity Framework 支持三种加载相关数据的方式——急切加载、延迟加载和显式加载。看看Loading Related Entities

      【讨论】:

      • 感谢您的链接。极大地帮助我。我用不同的方法解决了它
      • 不客气。请将答案标记为已接受(单击答案旁边的复选标记),以便对其他人有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多