简体   繁体   English

将 postgresql 查询转换为 asp.net 核心中的 entityframework 核心

[英]Translate an postgresql query to entityframework core in asp.net core

I am trying to translate the following Postgresql query in a dotnet api application using entityframework core in a way to return a list of objects containing the results in the output of that query.我正在尝试使用 entityframework 核心在 dotnet api 应用程序中翻译以下 Postgresql 查询,以返回包含该查询 output 中结果的对象列表。

sql query to translate: sql 查询翻译:

select speciality_type, speciality_priority , 
   count(speciality_type)
from specialties
group by speciality_priority, speciality_type

desired output:所需的 output:

在此处输入图像描述

knowing that the last column is the count column witch is not initially in the table and that I created in the query.知道最后一列是计数列,女巫最初不在表中,而是我在查询中创建的。 I want to get all these columns from _DataAccessObject.SpecialitiesTable and create from its values another list of an object that I create object(string type, int priority, int count)我想从_DataAccessObject.SpecialitiesTable中获取所有这些列,并从它的值创建另一个我创建object(string type, int priority, int count)

I tried a lot of stuff like _DataAccess.SpecialitiesTable.GroupBy(...).Select(...) most of them give me a error once function is called saying that the EF core statement could not be translated to sql.我尝试了很多类似_DataAccess.SpecialitiesTable.GroupBy(...).Select(...)的东西,一旦调用 function 说 EF 核心语句无法转换为 sql,它们中的大多数都会给我一个错误。

Thank you in advance.先感谢您。

Try the following query:尝试以下查询:

var result = await _DataAccess.SpecialitiesTable
   .GroupBy(s => new { s.speciality_type, s.speciality_priority })
   .Select(g => new 
   {
      g.Key.speciality_type,
      g.Key.speciality_priority,
      count = g.Count()
   })
   .ToListAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM