Linq到SQL转义WHERE子句

Linq to Sql escape WHERE clause
2020-10-17
  •  译文(汉语)
  •  原文(英语)

我有这个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.

Linq到SQL转义WHERE子句

当我有大数据时,出现内存不足异常的问题.

当我检查生成的查询时

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;
        }
    }
}
速聊1:
好吧,在.GetTable()我看来,该方法似乎是调用以获取整个表,这正是您所看到的...
速聊2:
GetTable()方法返回不调用get的"ITable".我试图删除查询行并保留Just GetTable()的结果将不会生成查询
速聊3:
你能告诉我们什么DaynamicContext吗?
速聊4:
我编辑问题以显示daynamicContext,而tbl对象是运行时生成的类
解决过程1

GetTable方法将首先获取整个表,然后从列表中选择第一个值.另外,也不要Take()使用FirstOrDefault().

速聊1:
GetTable()方法返回不调用get的"ITable".我试图删除查询行并保留Just GetTable()的结果将不会生成查询
速聊2:
尝试删除汇票?另外,为什么还要将其转换为可枚举然后转换为列表?

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.

Linq to Sql escape WHERE clause

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;
        }
    }
}
Talk1:
Well, the .GetTable() method seems to me to be a call to get the whole table which is exactly what you're seeing .....
Talk2:
GetTable() method Return "ITable" which is not invoke the get. I tried to remove the query line and keep Just GetTable() the result no query will generated
Talk3:
Can you show us what DaynamicContext is??
Talk4:
I edit the question to show the daynamicContext and the tbl object is runtime generated class
Solutions1

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

Talk1:
GetTable() method Return "ITable" which is not invoke the get. I tried to remove the query line and keep Just GetTable() the result no query will generated
Talk2:
tried removing the Take? Also Why are you converting it to as enumerable and then to a list?
转载于:https://stackoverflow.com/questions/30816160/linq-to-sql-escape-where-clause

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入