How to construct having clause in Dynamic LINQ

49 Views Asked by At

I want to find duplicate rows in a table given the list of columns. I am using Dynamic LINQ to group by columns and then want to check if there are any records having count greater than 1.

The group by function and count is working correctly. However, I am not sure how do I construct having clause.

Currently, I am getting list of group count in memory and then identify if there is any duplicate.

var columns = "new(FirstName, LastName)"
dynamic groups = await _dbContext.Users
                     .Where(x=>x.ClientID = 1234)
                     .GroupBy(columns)
                     .Select("new(Count() AS Count)")
                     .ToListAsync();

I trying to avoid loading list in memory. The query should return boolean like Any() if count is > 1

1

There are 1 best solutions below

0
On

I think i got it

var columns = "new(FirstName, LastName)"
var found = _dbContext.Users
                     .Where(x=>x.ClientID = 1234)
                     .GroupBy(columns)
                     .Select("new(Count() AS Count)").Where("Count > 1").Any();