title: How can i group data in asp.net?
body: <p>I used dependency Injection to get data and display in my view which all working great but i need your help to group my the result based on the ID field.</p>
  
  <p>In my View i have this:</p>
  
  <pre><code>@inject ICompanyService companyListService
  
  @{
      var ProductID = Model.ID;
      var compInfo = await companyListService.GetCompanyByID(ProductID); 
  }
  </code></pre>
  
  <p>To create a list i used  this code </p>
  
  <pre><code>&lt;ul id="list" style="line-height: 0.01;"&gt;
                @foreach (var item in compInfo)
                       {
                        &lt;li data-expanded="false" class="panel-handler" data-id="@item.ID"&gt;@item.CompanyName                             
                                &lt;ul&gt;
                                   &lt;li class="panel-handler" data-expanded="true" data-id="@subcontractor.ID"&gt;@scontractor.ContractorName&lt;/li&gt;
                                 &lt;/ul&gt;                                                                
                         &lt;/li&gt;
                      }
        &lt;/ul&gt;
  </code></pre>
  
  <p>my data looks like as shown below</p>
  
  <pre><code>ID     CompanyID   CompanyName   ContractorName
  255     13          MULETA PLC   YIBA ENGINEERING
  255     13          MULETA PLC   SUNS CONST.
  260     13          MULETA PLC   YIBA ENGINEERING
  260     13          MULETA PLC   SUNS CONST.
  </code></pre>
  
  <p>I need your help to group the above data to get the following result.</p>
  
  <pre><code>MULETA PLC
      &gt;&gt;&gt;YIBA ENGINEERING
      &gt;&gt;&gt;SUNS CONST.
  MULETA PLC
      &gt;&gt;&gt;YIBA ENGINEERING
      &gt;&gt;&gt;SUNS CONST.
  </code></pre>
  
  <p>My linq expression looks like </p>
  
  <pre><code> var CompanyInfo =  await Context.Company
                             .Where(b =&gt; b.ProductID == request.ID)
                             .ToListAsync();
  </code></pre>
  
  <p>I need your help i group the result by ID field.</p>
  
  <p>My API Controller</p>
  
  <pre><code>    [HttpGet("{ID}")]
      public async Task&lt;IActionResult&gt; GetCompanyByID(int produtID)
      {
          var company= await companyListService.GetCompanyByID(productID); 
          var companyList= company.GroupBy(x =&gt; x.ID);
          return Ok(companyList);
      }
  </code></pre>
  
  <p>And Result</p>
  
  <pre><code>[ [ { "ID": 13, "CompName": "MULETA PLC ", "ContractorName": "ENGINEERING" }, 
  { "ID": 13, "CompName": "MULETA PLC ", "ContractorName": "SUNS CONST." } ],
  </code></pre>