C#MVC中google图表的数组列表

C#MVC中google图表的数组列表,c#,arrays,asp.net-mvc,C#,Arrays,Asp.net Mvc,我必须创建一个数组,其中包含google图表饼图的数组列表 [['Status', 'Count'],['New',5], ['Converted',0],['Completed',0]] 我使用的一种方法实现起来最快,但有点脏: public class DashboardVM { public DashboardVM(IEnumerable<Lead> leadsForOrganisation) { this.leadsByAgent = ne

我必须创建一个数组,其中包含google图表饼图的数组列表

[['Status', 'Count'],['New',5], ['Converted',0],['Completed',0]] 
我使用的一种方法实现起来最快,但有点脏:

public class DashboardVM
{
    public DashboardVM(IEnumerable<Lead> leadsForOrganisation)
    {
        this.leadsByAgent = new List<LeadsByAgent>();
        this.leads = leadsForOrganisation;

        foreach (var groupedLeads in leadsForOrganisation.GroupBy(m=>m.contactId))
        {
            this.leadsByAgent.Add(new LeadsByAgent(groupedLeads));
        }

        string chart = "[['Status', 'Count'],['New'," + leadsForOrganisation.Where(m => m.leadStatusId == 1).Count() +
            "], ['Converted'," + leadsForOrganisation.Where(m => m.leadStatusId == 3).Count() +
            "],['Completed'," + leadsForOrganisation.Where(m => m.leadStatusId == 2).Count() + "]]";

        this.googleChartData = chart;

    }

    public List<LeadsByAgent> leadsByAgent { get; set; }
    public string googleChartData { get; set; }


    public IEnumerable<Lead> leads { get; set; }


}
第二种方法更干净,但对于这项任务来说似乎有些过头了

 public class DashboardVM
 {
    public DashboardVM(IEnumerable<Lead> leadsForOrganisation)
    {
        this.leadsByAgent = new List<LeadsByAgent>();
        this.leads = leadsForOrganisation;

        foreach (var groupedLeads in leadsForOrganisation.GroupBy(m=>m.contactId))
        {
            this.leadsByAgent.Add(new LeadsByAgent(groupedLeads));
        }
    }

    public List<LeadsByAgent> leadsByAgent { get; set; }


    public string gchartJavaScriptJson { get { return getJsonChart(); } }
    public IEnumerable<Lead> leads { get; set; }

    public string getJsonChart()
    {
        var data = new object[] {
            new object[]{"Status", "Count"},
            new object[]{"New", this.leads.Where(m => m.leadStatusId == 1).Count()},
            new object[]{"Contacted", this.leads.Where(m => m.leadStatusId == 2).Count()},
            new object[]{"Converted", this.leads.Where(m => m.leadStatusId == 3).Count()}
        };

        return  new JavaScriptSerializer().Serialize(data);
    }
}

有没有更好的清洁方法的想法?

我认为第一种方法看起来足够清洁

您可以根据状态对潜在客户进行分组,以获得每个潜在客户的数量:

Dictionary<int, int> leadsByStatus =
  leadsForOrganisation.GroupBy(m => m.leadStatusId)
  .ToDictionary(g => g.Key, g => g.Count());
 function drawChart() {
        var data = google.visualization.arrayToDataTable(
        @Html.Raw(Model.gchartJavaScriptJson)
        );

        var options = {
            title: '',
            is3D: true,
            backgroundColor: '#F9F9F9',
            width:500,
            height:300,
          };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));
        chart.draw(data, options);
    }
Dictionary<int, int> leadsByStatus =
  leadsForOrganisation.GroupBy(m => m.leadStatusId)
  .ToDictionary(g => g.Key, g => g.Count());
string chart = String.Format(
  "[['Status', 'Count'],['New',{0}], ['Converted',{1}],['Completed',{2}]]",
  leadsByStatus[1], leadsByStatus[3], leadsByStatus[2]
);