I'm trying to convert a sql server query to Mongo C# driver LINQ.
I have a table with multiple entries for each guid
and each entry has different code
value.
I need get a distinct count of guids for each code. I'm able to do Count()
in LINQ but when I do Distinct().Count()
I get the following exception:
Specified method is not supported
Following is the T-SQL I'm trying to convert:
SELECT a.code, substring(convert(varchar, a.create_Date, 107), 1, 6) as create_Date,count(distinct a.guid) as mycount
FROM table1 a (nolock)
WHERE a.code = 0
AND a.create_Date BETWEEN DateAdd(day, -2 , @startDate) AND DateAdd(day, 1, @startDate)
GROUP BY a.code, substring(convert(varchar, a.create_Date, 107), 1, 6)
Order by 1
Following is my LINQ :
var result = from tb in collection3.AsQueryable()
where
tb.code == 0
&& tb.create_Date > rptGte
&& tb.create_Date < rptLt
group tb by new
{
tb.code,
create_Date = tb.create_Date.DayOfYear
} into g
select new
{
code = g.Key.code,
create_Date = g.Key.create_Date,
mycount = g.Select(x => x.guid).Distinct().Count() //distinct not working here
};
var resultList = rpt.ToList();
Please tell what am I doing wrong here.