I have a large table (~3 million rows) that I'm querying for just a few records using a distinct column (OperationTypeId). In SQL Management Studio, I'm using the following query which executes in 4 seconds and return 7 rows.
SELECT *
FROM (SELECT t.ReferenceNumber, t.OperationTypeId,
ROW_NUMBER() OVER (PARTITION BY OperationTypeId
ORDER BY OperationTypeId) AS RowNumber
FROM TP_Transaction t) AS a
WHERE a.RowNumber = 1
I'm trying to accomplish the same result in code using LINQ to SQL, and this is my code.
var temp = dbConn.TP_Transactions
.GroupBy(x => x.OperationTypeId)
.Select(g => new {OperationTypeId = g.Key, ReferenceNumber = g.FirstOrDefault().ReferenceNumber})
.ToList();
However, this code results in an exception:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Please advise on what I can do to accomplish this query.
I ended up doing this instead. It still takes a significant amount of time to run (~2 minutes) but at least I'm getting some results back.
const string sqlQuery = @"SELECT *
FROM (SELECT t.ReferenceNumber,
ROW_NUMBER() OVER (PARTITION BY OperationTypeId ORDER BY OperationTypeId) AS RowNumber
FROM TP_Transaction t) AS a
WHERE a.RowNumber = 1";
var temp = dbConn.ExecuteQuery<Temp>(sqlQuery).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.