简体   繁体   English

linq to sql中的奇怪行为,无需过滤即可返回所有行

[英]strange behavior in linq to sql to return all rows without filtering

I write this code: 我写这段代码:

Rep_Regions clsr = new Rep_Regions();
Func<Regions, bool> filter = r => r.RegionID == int.Parse(textBox5.Text);
Regions reg = new Regions();
reg = clsr.FindSingle(filter);

and : 并且:

 public Regions FindSingle(Func<Regions, bool> exp)
    {
        using (RepositoryDataContext = new DataClasses1DataContext())
        {
            return RepositoryDataContext.Regions.Where(exp).FirstOrDefault();
        }
    }

this is the query that execute in Sql Server: 这是在Sql Server中执行的查询:

SELECT [t0].[RegionID], [t0].[RegionDescription]
FROM [dbo].[Region] AS [t0]

Why the query not filter results and return all rows? 为什么查询不会过滤结果并返回所有行?

You have used Func<Regions, bool> filter which forces it to use LINQ-to-Objects after running an unfiltered query at the server. 您已经使用了Func<Regions, bool> filter ,它强制它在服务器上运行未过滤的查询使用LINQ-to-Objects。 To use query composition you must use expression-trees, not delegates. 要使用查询组合,您必须使用表达式树,而不是委托。

Change it to: 将其更改为:

int regionId= int.Parse(textBox5.Text);
Expression<Func<Regions, bool>> filter = r => r.RegionID == regionId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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