1

I have a PredicateBuilder which I got it from http://www.albahari.com/nutshell/predicatebuilder.aspx

And here is how I use it:

var Expression = PredicateBuilder.True<UCM_UserAgent>();
Expression = Expression.And(item => item.AgentText == "TestUserAgent Value");

Func<UCM_UserAgent, bool> SearchCriteria = Expression.Compile();
var Data = Context.UCM_UserAgent
                                    .Where(SearchCriteria)
                                    .ToList();

And when I checked SQL Profiler on my database:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[AgentText] AS [AgentText], 
FROM [dbo].[UCM_UserAgent] AS [Extent1]

Now, where is the condition I added? I could not solve this. Unfortunately, this Entity Framework and Predicate Builder - Predicates being Ignored in SQL Query did not help either.

Edit: To eliminate any misunderstandings; I do not want to use as x => x.AgentText = "SomeText". Because I want to build a dynamic LINQ query, thus I use PredicateBuilder.

Community
  • 1
  • 1
E-A
  • 1,995
  • 6
  • 33
  • 47

1 Answers1

5

If you want the predicate to be hadled by LINQ to Entities you need to pass the Expression<Func<T,bool>> version to the Queryable.Where() method instead of passing the Func<T,bool> version to the Enumerable.Where() method as you are currently doing, i.e. do not compile the expression into IL before calling the Where method or you will necessarily get the LINQ to Objects version.

divega
  • 6,320
  • 1
  • 31
  • 31