Note that there are some explanatory texts on larger screens.

plurals
  1. POObject Type conversion while doing Group By
    primarykey
    data
    text
    <p>I have a List. I need to find the unique ExistingData records by applying Group By. Following code works.</p> <pre><code> var distinctItemsWorking = myCostPages .GroupBy(x =&gt; new { x.CostPageContent.Program, x.CostPageContent.Group, x.CostPageContent.Sequence }) .Select(y =&gt; y.First()); </code></pre> <p>Now I need to convert the unique list into a List. How can we achieve this conversion when we do Grouping?</p> <p><strong>C# Method</strong></p> <pre><code> public List&lt;CostPage&gt; GetCostPages(SearchEntity search, int pageIndex, int pageSize) { List&lt;ExistingData&gt; AllData = GetExistingData(); var allMatchingValues = from existingDatas in AllData where existingDatas.CostPageContent.Program == search.Program select existingDatas; var query = allMatchingValues; List&lt;ExistingData&gt; currentSelectionForExistingData = query .Skip(pageIndex * pageSize) .Take(pageSize) .ToList(); //var distinctItems = currentSelectionForExistingData.GroupBy(x =&gt; new { x.CostPageContent.Program, x.CostPageContent.Group, x.CostPageContent.Sequence }) // .Select(y =&gt; new CostPage() // { // CostPageContent = y.CostPageContent // } // ); var distinctItemsWorking = currentSelectionForExistingData.GroupBy(x =&gt; new { x.CostPageContent.Program, x.CostPageContent.Group, x.CostPageContent.Sequence }) .Select(y =&gt; y.First()); List&lt;CostPage&gt; myCostPages = new List&lt;CostPage&gt;(); foreach (ExistingData exist in distinctItemsWorking) { CostPage c = new CostPage(); c.CostPageContent = exist.CostPageContent; myCostPages.Add(c); } return myCostPages; } </code></pre> <p><strong>Other Classes</strong></p> <pre><code>public class ExistingData { public CostPageNumberContent CostPageContent { get; set; } public string ItemID { get; set; } } public class CostPage { public CostPageNumberContent CostPageContent { get; set; } } public class CostPageNumberContent { public string Program { get; set; } public string Group { get; set; } public string Sequence { get; set; } } public class SearchEntity { public string Program { get; set; } public string Sequence { get; set; } public string ItemID { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload