11
I have the following consultation at T-SQL
:
SELECT TOP (60) COUNT(*) AS QTD, SUM(Duration) AS Total, AVG(Duration) AS Media, MIN(Duration) AS Menor, LEFT (TextData, 80) AS TextData
FROM Traces WITH (nolock)
WHERE (StartTime >= DATEADD(D, - 7, GETDATE())) AND (DataBaseName IN ('databaseName1', 'databaseName2'))
GROUP BY TextData, DataBaseName
HAVING (COUNT(*) > 1) ORDER BY Total DESC
I’m trying to write in Lambda, I wouldn’t like to Linq
, only if this is the last case.
How to ride?
I have doubts as to the LEFT
and to the HAVING
:
this is my consultation for now.
using (var ctx = new TracesEntity())
{
var lambda = ctx.TraceTabelas.Where(q => q.DataBaseName == "databaseName1" || q.DataBaseName == "databaseName2")
.GroupBy(a => new { a.TextData, a.DataBaseName })
.Select(b => new {
QTD = b.Count(),
Total = b.Sum(b1 => b1.Duration),
Media = b.Average(b1 => b1.Duration),
Menor = b.Min(b1 => b1.Duration),
TextData = b.Select(b1 => b1.TextData)
})
.Take(60)
.OrderByDescending(b =>b.Total)
.ToList();
GridView1.DataSource = lambda;
GridView1.DataBind();
}
You should order first and then give the
take
– Andre Figueiredo
I would strongly recommend that you keep this kind of consultation in the bank, through
Stored Procedures
to gain performance in the query instead of having q do the relations in the code like this.– Tafarel Chicotti
lambda doesn’t have a good performance? of course many things are already stored in Process, but you can’t create everything.. until pq this is a page that runs only 1 time a day.
– Dorathoto
How is the result so far?
– Leonel Sanches da Silva
textData is not coming.. and other things must be wrong, for example where do I have it??? , there is as in debug I check which query he is riding on TSQL ?
– Dorathoto
Yes. Remove the
ToList()
and theTake(60)
.lambda
will receive a query object.– Leonel Sanches da Silva