[英]How do I get the result of SQL Query in C# Entity Framework?
I'm using WPF, C#, Entity Framework database-first with a SQL Server database.我正在使用 WPF、C#、Entity Framework 数据库,首先使用 SQL 服务器数据库。
I could execute this query successfully in SQL Server:我可以在 SQL 服务器中成功执行此查询:
SELECT
SUM(MEGHk) AS Meghksm,
SUM(MABL_K) AS Mablksm,
SUM(N_MOIN) AS N_MOINSM,
SUM(IMBAA) AS IMBAAsm
FROM
INVO_LST
WHERE
(TAG = 2) AND (NUMBER = 4)
In C# code:在 C# 代码中:
var quer_Sumfactor5 = dbms.Database.SqlQuery<INVO_LST>("SELECT SUM(MEGHk) AS Meghksm, SUM(MABL_K) AS Mablksm, SUM(N_MOIN) AS N_MOINSM, SUM(IMBAA) AS IMBAAsm FROM INVO_LST WHERE(TAG = 2) AND (NUMBER = " + TextB_Number.Text + ")").ToList();
Which returns this error:返回此错误:
You are performing aggregation on some of the columns in your query and have aliased the results of these aggregations.您正在对查询中的某些列执行聚合,并对这些聚合的结果进行了别名。 As a result the columns returned (
Meghksm
, Mablksm
etc.) are not the same as those in the database table INVO_LST
that your C# class INVO_LST
is a representation of.因此,返回的列(
Meghksm
、 Mablksm
等)与您的 C# class INVO_LST
表示的数据库表INVO_LST
中的列不同。 So Entity Framework cannot map the results returned to the class that you have specified in your dbms.Database.SqlQuery<INVO_LST>
call.所以实体框架不能 map 将结果返回到您在
dbms.Database.SqlQuery<INVO_LST>
调用中指定的 class 。
You need to create a class that EF can map to the results of the query:您需要创建一个 class EF 可以 map 到查询结果:
public class INVO_LST_SUM
{
public decimal Meghksm {get; set;}
public decimal Mablksm {get; set;}
public decimal N_MOINSM {get; set;}
public decimal IMBAAsm {get; set;}
}
Then you can call:然后你可以调用:
var quer_Sumfactor5 = dbms.Database.SqlQuery<INVO_LST_SUM>("SELECT SUM(MEGHk) AS Meghksm, SUM(MABL_K) AS Mablksm, SUM(N_MOIN) AS N_MOINSM, SUM(IMBAA) AS IMBAAsm FROM INVO_LST WHERE(TAG = 2) AND (NUMBER = " + TextB_Number.Text + ")").ToList();
(although please carefully note @Liam 's comment re SQL injection...) (虽然请仔细注意@Liam 的评论关于 SQL 注射......)
first of all you should read the sql injection.首先,您应该阅读 sql 注入。 You should not use the " TextB_Number.Text " in your query, as it can result in less data security.
您不应在查询中使用“TextB_Number.Text”,因为它会降低数据安全性。 Secondly,
dbms.Database.SqlQuery<INVO_LST>
SqLQuery expects a class that can match the type and name of the columns that will come in query result.其次,
dbms.Database.SqlQuery<INVO_LST>
SqLQuery 需要一个 class 可以匹配查询结果中的列的类型和名称。 So you should use following approach.所以你应该使用以下方法。
public class INVO_LST_SUM
{
public decimal Meghksm {get; set;}
public decimal Mablksm {get; set;}
public decimal N_MOINSM {get; set;}
public decimal IMBAAsm {get; set;}
}
and和
var quer_Sumfactor5 = dbms.Database.SqlQuery<INVO_LST_SUM>("SELECT SUM(MEGHk) AS Meghksm, SUM(MABL_K) AS Mablksm, SUM(N_MOIN) AS N_MOINSM, SUM(IMBAA) AS IMBAAsm FROM INVO_LST WHERE(TAG = 2) AND (NUMBER = " + TextB_Number.Text + ")").ToList();
Use decimal instead of int,, As the aggregate function Sum can results in Decimal value also.使用十进制而不是 int,因为聚合 function 总和也可以导致十进制值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.