简体   繁体   中英

Linq to Sql escape WHERE clause

I have this Linq-to-SQL query

  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();

The result of final query is correct is got only single Id.

在此处输入图片说明

The problem when I have big data I got out of memory exception.

When I checked the generated query

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]

it seems like Linq-to-SQL is getting all data from database and filtering it on memory.

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;
        }
    }
}

The GetTable method will fetch the whole table first and then select the first value from the list. Also instead of using Take() , use FirstOrDefault() .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM