使用一个模型发送两个LINQ查询来查看

时间:2016-08-19 14:26:06

标签: c# linq asp.net-mvc-5

我需要在图表上显示这两个查询的数据。我可以在条形图上一次显示一个数据。但我想一起在同一页面上显示它们。

public ActionResult Daily(string label)
{
    var DataContext = new BalanceDataContext();

    var month = label.Split('-')[0];
    var year = label.Split('-')[1];

    //Percentage of Estimated Electric Bills 
    var rdcd = new string[] { "CE", "IE" };
    var totalEstimated = (from a in DataContext.pujhaccds
                           join b in DataContext.pujhmtrds
                           on a.billnumber equals b.billnumber
                           where year.Equals(a.billdate.Value.Year)
                           && month.Equals(a.billdate.Value.Month)
                           && rdcd.Contains(b.reading_code)
                           group a by new {a.billdate.Value.Day} into p
                           orderby p.Key.Day ascending
                           select new Date1() { theDay2 = p.Key.Day, theCount2 = p.Count() });


    var totalday = (from m in DataContext.pujhaccds
                      where month.Equals(m.billdate.Value.Month) &&
                      year.Equals(m.billdate.Value.Year)
                      group m by new { m.billdate.Value.Day } into p
                      orderby p.Key.Day ascending
                      select new Date1() { theDay = p.Key.Day, theCount = p.Count() });

    int monthInt = 0;
    Int32.TryParse(month, out monthInt);
    string strMonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthInt);
    ViewBag.Message = "Bills for: " + strMonthName + year;

    return View(new QueryView { Date2 = totalEstimated, Date1 = totalday.ToArray() });
}

这是模型的样子:

public class Date1
{   
public string Month { get; set; }
public string Year { get; set; }

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public int theDay { get; set; }
public int theDay2 { get; set; }
public string theString { get; set; }

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MMMM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? theDate { get; set; }
public int theCount { get; set; }
public int theCount2 { get; set; }
}

我尝试创建另一个模型来保存数据,但它似乎无法正常工作

public class QueryView
{
    public IEnumerable<Date1> Date1 { get; set; }
    public IEnumerable<Date1> Date2 { get; set; }
}

我在我的视图顶部有这个,没有错误消息:

@model CWebPortal.Models.QueryView

当我这样做时,我遇到了错误:

@model IEnumerable<CWebPortal.Models.QueryView>

这样可行,但仅适用于来自控制器的一个查询:

@model IEnumerable<CWebPortal.Models.Date1>

这就是我在视图中抓取图表数据的方式:

    jQuery(function ($) {
        $.jqplot.config.enablePlugins = true;
        var items = @Html.Raw(Json.Encode(Model));
        var ticks = [];
        var s1 = [];
        var s2 = [];
        for (var i = 0; i < items.length; i++) {
            ticks.push(items[i].theDay);
            s1.push(items[i].theCount);
        }

如何将两个查询都放入我的视图中?

1 个答案:

答案 0 :(得分:1)

将它们组合到新的View模型中的方法绝对正确。

public class QueryView
{
    public IEnumerable<Date1> Date1 { get; set; }
    public IEnumerable<Date1> Date2 { get; set; }
}

@model CWebPortal.Models.QueryView在您的视图中。

请确保您在javascript代码中访问正确的内容:

  jQuery(function ($) {
    $.jqplot.config.enablePlugins = true;
    var viewModel = @Html.Raw(Json.Encode(Model)); // <--- updated
    var items = viewModel.Date1;  // <--- updated
    var ticks = [];
    var s1 = [];
    var s2 = [];
    for (var i = 0; i < items.length; i++) {
        ticks.push(items[i].theDay);
        s1.push(items[i].theCount);
    }