Linq / Group and Count ?

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
castlesoft
Posts: 1
Joined: Thu 15 Feb 2018 02:31

Linq / Group and Count ?

Post by castlesoft » Thu 15 Feb 2018 02:47

How would the following translate into the EntityDac Linq syntax ?

Code: Select all

select Username, count(Username) as cnt
from MacAddresses
group by Username
I'm guessing something like:

Code: Select all

var
   Query : ILinqQueryable;
   Mac : IMacaddressExpression;
begin
   Mac := MacDataContext1.Macaddress;
   Query := From(Mac).GroupBy(Mac.Username).Select([Mac.Username]);
   EntityDataSet1.SourceCollection := MacDataContext1.GetEntities(Query);
How do you do the Count in the Select ?

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Linq / Group and Count ?

Post by MaximG » Mon 19 Feb 2018 09:57

The samples of working with .GroupBy and .Count expressions can be found in the EntityDemo project distributed with EntityDAC. However, the possibilities of .Count do not allow directly build a LINQ query to MacAddresses, which returns the same result as the SQL query you provided. The .Count expression in EntityDAC can count either the number of elements in the collection returned by the .Select expression, or the number of elements in the linked collection.

Post Reply