简体   繁体   English

如何将这个简单的实体框架查询转换为标准 SQL 查询?

[英]How to convert this simple Entity Framework query into a standard SQL query?

I have no experience with the .NET Entity Framework and I have some doubts about what exactly do this query:我没有使用 .NET Entity Framework 的经验,我对这个查询的具体操作有一些疑问:

using (MyCorpo.EarlyWarnings.Model.EarlyWarningsEntities context = new Model.EarlyWarningsEntities()) 
{
    DBContext.SetTimeout(context);    

    model.VulnerabilitySeverityAverage = (from x in context.VulnerabilityAlertDocuments select x.Severity).Average();
}

(Where the type of the model.VulnerabilitySeverityAverage is simply a string) (其中模型的类型.VulnerabilitySeverityAverage 只是一个字符串)

So I think that VulnerabilityAlertDocuments map the VulnerabilityAlertDocument database table because into the EarlyWarningsEntities I have something this line:所以我认为VulnerabilityAlertDocuments映射了VulnerabilityAlertDocument 数据库表,因为在EarlyWarningsEntities 中我有以下内容:

public DbSet<VulnerabilityAlertDocument> VulnerabilityAlertDocuments { get; set; }

So I am executing a query on the VulnerabilityAlertDocuments DbSet object that represent a query on my VulnerabilityAlertDocument table on my database.因此,我正在对VulnerabilityAlertDocuments DbSet 对象执行查询,该对象表示对我的数据库中的VulnerabilityAlertDocument表的查询。 Is it correct?这是正确的吗?

So what exatly do the previous query?那么前面的查询究竟做了什么?

I think that it select the Severity field value of all records in the VulnerabilityAlertDocument table and calculate the avarage value from all these value.我认为它选择了VulnerabilityAlertDocument表中所有记录的Severity字段值,并从所有这些值中计算出平均值。

Is it my reasoning correct?我的推理正确吗?

How can I convert this entity query in a classic SQL query?如何在经典 SQL 查询中转换此实体查询? Someone can help me?有人可以帮助我吗?

Tnx Tnx

How can I convert this entity query in a classic SQL query?如何在经典 SQL 查询中转换此实体查询?

To see actual SQL you can just call .ToString() method on your query;要查看实际的 SQL,您只需在查询中调用.ToString()方法即可;

var sql = (from x in context.VulnerabilityAlertDocuments select x.Severity).Average().ToString();

So I am executing a query on the VulnerabilityAlertDocuments DbSet object that represent a query on my VulnerabilityAlertDocument table on my database.因此,我正在对 VulnerabilityAlertDocuments DbSet 对象执行查询,该对象表示对我的数据库中的 VulnerabilityAlertDocument 表的查询。 Is it correct?这是正确的吗?

Yes是的

So what exatly do the previous query?

Your query will average value in Severity column of ValnerabilityAlertDocuments table.您的查询将在 ValnerabilityAlertDocuments 表的 Severity 列中取平均值。 your translated query would've looked simular to this:您翻译的查询看起来与此类似:

SELECT 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    AVG([Extent1].[Severity]) AS [A1]
    FROM [dbo].[ValnerabilityAlertDocuments] AS [Extent1]
)  AS [GroupBy1]

Also you could try to use such tool as SQL Server Profiler您也可以尝试使用诸如SQL Server Profiler 之类的工具

UPDATE: Just adding LinqPad to list of tools (thanks to Dismissile)更新:只需将LinqPad添加到工具列表中(感谢Dismissile

Select Average(x.Severity)
From VulnerabilityAlertDocuments x

Thats assuming your table is called "VulnerabilityAlertDocuments"那是假设你的表被称为“VulnerabilityAlertDocuments”

try again再试一次

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

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