IDataStore<TModel>. GroupBy<TKey, TElement>(Func<TModel, TKey> keySelector, Func<TModel, TElement> elementSelector) Method
.NET Standard 2.x
Groups rows of the primary buffer by the level of grouping criteria of DataStore. The sort criteria can be specified.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
IEnumerable<IGrouping<TKey, TElement>> GroupBy<TKey, TElement>(Func<TModel, TKey> keySelector, Func<TModel, TElement> elementSelector);
Type Parameters
TKey
TElement
Parameters
keySelector
Func<TModel, TKey>
A Func<Object, TModel> used to define the grouping criteria.
elementSelector
Func<TModel, TElement>
A transform function to apply to each row.
Returns
IEnumerable<IGrouping<TKey, TElement>>
Returns an IEnumerable<IGrouping<TKey,TElement>>
object which can be used to read data in groups.
Examples
The 'd_department_course' contains two levels of groups: the primary group is grouped by course_departmentid; the nested group is grouped by course_courseid. The following code example demonstrates how to get the primary group and return the data for the specified element.
using Appeon.ApiDoc.Models;
using DWNet.Data;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Appeon.ApiDoc.IDataStore_GenericExamples
{
public class GroupByExample
{
private readonly SchoolContext _context;
public GroupByExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example3()
{
// Instantiates a DataStore object with datawindow: d_department_course.
var datastore = new DataStore<D_Department_Course>(_context);
datastore.Retrieve();
// The primary group is by course_departmentid which has only 4 values.
var departmentGroup = datastore.GroupBy(a =>
a.Course_Departmentid,
b => new { b.Course_Title });
Console.WriteLine("Department Group Count: {0}", departmentGroup.Count());
/*This code produces the following output:
Department Group Count: 4
*/
}
}
public class Groupby_Key
{
public int Course_Departmentid { get; set; }
}
public class Groupby_Comparer : IEqualityComparer<Groupby_Key>
{
public bool Equals(Groupby_Key x, Groupby_Key y)
{
return x.Course_Departmentid == y.Course_Departmentid;
}
public int GetHashCode(Groupby_Key obj)
{
return obj.Course_Departmentid.GetHashCode();
}
}
}
Example Refer To
Model Class: D_Department_Course
DataWindow File: d_department_course