2013-12-17 5 views
3

내가있는 개체를 가지고Linq에 선택 제품 수 년 카테고리

enter image description here 아래처럼 구글 막대 그래프 플롯하려고에 따라

나는

var grouped = (
    from c in allCategories 
from p in allProducts 

group p by new { year = p.CreateDate.Year} into d 

select new { year = d.Key.year, count = d.Count()}).OrderByDescending(g => g.year).Distinct(); 
<script type="text/javascript"> 
    google.load("visualization", "1", { packages: ["corechart"] }); 
    google.setOnLoadCallback(drawChart); 
    function drawChart() { 
     var data = google.visualization.arrayToDataTable([ 
      ['Category Name', '2006', '2011', '2013'], 

      @foreach (var p in grouped) 
      { 
       int productThisYear = grouped.Where(x => x.year <= p.year).Select(x => x.count).Sum(); 

       <text> 
       ['', @productThisYear,@productThisYear,@productThisYear] 
       </text> 
       if(counter++ != grouped.Count() - 1){ 
        @Html.Raw(",") 
       } 
      } 
     ]); 

     var options = { 
      title: 'Product Categories', 
      hAxis: { title: '' } 
     }; 

     var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 
    } 
    </script> 
과 같은 코드를 시도
product 
{ 
id, 
name, 
categoryid 
} 

category 
{ 
id, 
categoryname 
} 

하지만 범주 이름과 제품 수를이 범주 이름과 연도를 기반으로 가져올 수 없습니다. 어떤 문제가 발생할 수 있습니까? 도움에 미리 감사합니다 ..

답변

3

당신은 그래서 당신은 p.categoryname

로 foreach는 카테고리 이름을 얻을 수있는이

var grouped = (
    from c in allCategories 
    join p in allProducts on c.id equals p.categoryid 
    group p by new {p.CreateDate.Year, c.categoryname} into g 
    select new {categoryname=g.Key.categoryname, g.Key.Year, count=g.Count()}) 
    .OrderByDescending(g => g.year); 

같은 join clause을 사용할 수 있습니다

관련 문제