Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ENTITY FRAMEWORK GRoupBy,OrderBy,Sum

I am trying to rewrite this querry in entity framework (Method syntax) but i can’t,I don’t understand what needs to be done first.
My postgres Querry:

select  id, Sum(yellow_hour)"yellow_hour"
from my_table
group by id
where date_part('day', date::timestamptz - '2022-10-30 00:00:00.000 +0200'::timestamptz)>0
ORDER by yellow_hour desc 
limit 20

My attempt:

var sensors=dbContext.mytable.Where(x=>x.Date >= instantStartDate && x.Date<= instantEndDate).GroupBy(x=>x.id).ToList();

Error
Unable to translate the given ‘GroupBy’ pattern. Call ‘AsEnumerable’ before ‘GroupBy’ to evaluate it client-side.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Although not having access to a compiler right now there might be some typo’s but you will get the idea with the current LINQ query.

var queryDate = DateTime.Parse("2022-10-30 00:00:00.000 +0200");
db.my_table.Where(t=>t.date > queryDate).GroupBy(t => t.id).
              Select(g => new 
                {
                    g.Key, 
                    SUM = g.Sum(s=>s.yellow_hour)
                }).OrderByDescending(g=>g.SUM).Take(20);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading