在VS2015中使用C#重新創建的LINQ to Entities查詢中DBO
我有一個正在太長了LINQ查詢,直到我能解決這個問題,我需要繼續前進。 我有一個返回很多記錄的查詢,然後我使用其他LINQ查詢針對返回的數據爲關閉的工作,像這樣:
var Stops = (from stops in rDb.DistributionStopInformations
join line in rDb.DistributionLineItems on stops.UniqueIdNo equals line.UniqueIdNo
where line.DatetimeCreated > dateToCheck && stops.CustomerNo == TNGCustNo
select new
{
stops.UniqueIdNo,
stops.StopName,
stops.StopAddress,
stops.RouteCode,
stops.CustomerReference,
line.DatetimeCreated,
line.RmaNumber
}
).ToArray();
int stopsCount = Stops.Count();
var RMA_stops = (from rma in Stops where rma.RmaNumber != null select rma).ToArray();
然後我就可以使用RMA_stops(與許多其他一起)而不必一遍又一遍地返回數據庫。
這工作正常,但停止查詢是從一分鐘到超過5分鐘的任何地方運行,只是不工作。
我基本上重新創建了ADO中的停止查詢,它運行在大約一秒鐘的時間內,這很好 - 但是我對ADO的知識卻令人尷尬地很差......我如何將輸出從下面轉換爲我可以運行的東西對,用各自的領域(強類型的?)
string mySelectQuery = "select distribution_stop_information.unique_id_no, stop_name, stop_address, route_code, customer_reference,"+
"distribution_line_items.datetime_created, rma_number from distribution_stop_information join distribution_line_items on " +
"distribution_line_items.unique_id_no = distribution_stop_information.unique_id_no " +
"where distribution_line_items.datetime_created > '2/22/2017' and customer_no = '91000'";
PgSqlConnection pgConnection = new PgSqlConnection(myConnection);
PgSqlCommand pgCommand = new PgSqlCommand(mySelectQuery, pgConnection);
pgConnection.Open();
PgSqlDataReader pgReader = pgCommand.ExecuteReader();
//steps through it just to verify that the data is in fact coming back
try
{
int counter = 0;
while (pgReader.Read())
{
counter++;
Console.WriteLine(counter.ToString() + " " + pgReader.GetString(0));
}
}
你有沒有考慮過使用[短小精悍(https://github.com/StackExchange/Dapper)對於性能關鍵查詢?它通過'Query'函數擴展'IDbConnection'接口,返回'IEnumerable '。 –
採用原始SQL路徑會產生比使用L2E更多的麻煩。此外,爲了需要原始SQL優化,L2E查詢非常簡單。從我所看到的,唯一的區別是L2E將使用SQL參數,而您的手寫SQL查詢包含嵌入的常量值。如果來自這種情況,最有可能的區別 - 爲什麼不嘗試使用常量的LINQ查詢,看看是否是這種情況。使LINQ綁定常量比實現原始SQL要容易得多(例如,不能使用匿名類型)。 –