实体框架和谓词生成器-谓词在SQL查询中被忽略 [英] Entity Framework and Predicate Builder - Predicates being Ignored in SQL Query

查看:118
本文介绍了实体框架和谓词生成器-谓词在SQL查询中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用EF和Predicate Builder时遇到问题.我已经看完所有说明,并且可以肯定我做的一切都正确,但是当我运行SQL Profiler并检查去往数据库的查询时,它会忽略我的谓词并获取表中的每条记录,而这表目前最多可容纳600,000行,因此它会稍微减慢速度.在数据库查询后,我的谓词就会被应用.

I'm having a problem with EF and Predicate Builder. I've been through all the instructions and I'm pretty sure I'm doing everything right, but when I run SQL Profiler and inspect the query going to the database, it's ignoring my predicates and getting every record in the table, and this table is currently up to about 600,000 rows so it slows things down a little. My predicates then get applied after the database has queried.

有人可以告诉我我想念什么吗?

Can someone please tell me what I'm missing?

var predicate = PredicateBuilder.True<ListRecord>();

var classFilter = PredicateBuilder.False<ListRecord>();
classFilter = classFilter.Or(x => x.Community == "Air Force");
classFilter = classFilter.Or(x => x.Community == "Navy");
predicate = predicate.And(classFilter);

// Add several more predicates just like classFlter

var query = db.ListRecords.AsExpandable().Where(predicate.Compile());
var list = query.ToList();

我复制的示例是 http://www.albahari.com上的嵌套谓词/nutshell/predicatebuilder.aspx

这是正在生成的SQL:

This is the SQL that's being produced:

SELECT 
[Extent1].[ListRecordId] AS [ListRecordId], 
[Extent1].[Community] AS [Community]
-- And every other column from this table
FROM [dbo].[ListRecord] AS [Extent1]

推荐答案

我认为您不需要创建的第一个谓词.这样会不会返回您需要的结果?

I don't think you need the first predicate you create. Wont this return the results you need?

var classFilter = PredicateBuilder.False<ListRecord>();
classFilter = classFilter.Or(x => x.Community == "Air Force");
classFilter = classFilter.Or(x => x.Community == "Navy");

// Add several more predicates just like classFlter

var query = db.ListRecords.AsExpandable().Where(classFilter);
var list = query.ToList();

或带有其他谓词,例如Classfilter:

Or with additional predicates like Classfilter:

var classFilter = PredicateBuilder.False<ListRecord>();
classFilter = classFilter.Or(x => x.Community == "Air Force");
classFilter = classFilter.Or(x => x.Community == "Navy");

var list = db.ListRecords.AsExpandable()
    .Where(classFilter)
    .Where(someOtherFilterLikeClassFilter)
    .Where(AnotherOneLikeClassFilter)
    .ToList();

这篇关于实体框架和谓词生成器-谓词在SQL查询中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆