[英]Linq to Sql escape WHERE clause
我有這個Linq-to-SQL查詢
DaynamicContext.Log = new System.IO.StreamWriter("f:\\linq-to-sql.txt") { AutoFlush = true })
var tt = DaynamicContext.GetTable(tbl);
var query = ((from c in tt where c.IsDeleted != true select c.Id).Take(1)).ToList();
最終查詢的結果正確,只有一個ID。
當我有大數據時,出現內存不足異常的問題。
當我檢查生成的查詢時
SELECT [t0].[Id], [t0].[CreatedBy], [t0].[CreatedDate], [t0].[ModifiedBy],
[t0].[ModifiedDate], [t0].[IsDeleted], [t0].[UntileTime], [t0].[Desktop],
[t0].[Laptop], [t0].[Title], [t0].[Responsive], [t0].[Mobile], [t0].[ActiveTime],
[t0].[Tablet]
FROM [Countent_FreeArea] AS [t0]
看來Linq-to-SQL從數據庫中獲取所有數據並將其過濾到內存中。
public class Context
{
private DataContext daynamicContext;
public DataContext DaynamicContext
{
get
{
if (daynamicContext == null)
{
System.Configuration.ConnectionStringSettingsCollection connectionStrings = WebConfigurationManager.ConnectionStrings;
daynamicContext = new DataContext(connectionStrings["connectionStrings"].ToString());
}
return daynamicContext;
}
}
}
GetTable方法將首先獲取整個表,然后從列表中選擇第一個值。 同樣,不要使用Take()
,而要使用FirstOrDefault()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.