문제

I have no experience with the .NET Entity Framework and I have some doubts about what exactly do this query:

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)

So I think that VulnerabilityAlertDocuments map the VulnerabilityAlertDocument database table because into the EarlyWarningsEntities I have something this line:

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. 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.

Is it my reasoning correct?

How can I convert this entity query in a classic SQL query? Someone can help me?

Tnx

도움이 되었습니까?

해결책

How can I convert this entity query in a classic SQL query?

To see actual SQL you can just call .ToString() method on your query;

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. Is it correct?

Yes

So what exatly do the previous query?

Your query will average value in Severity column of ValnerabilityAlertDocuments table. 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

UPDATE: Just adding LinqPad to list of tools (thanks to Dismissile)

다른 팁

Select Average(x.Severity)
From VulnerabilityAlertDocuments x

Thats assuming your table is called "VulnerabilityAlertDocuments"

try again

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top