Print Page | Close Window

update to problem with PredicateBuilder

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2054
Printed Date: 12-Oct-2025 at 9:53pm


Topic: update to problem with PredicateBuilder
Posted By: BillG
Subject: update to problem with PredicateBuilder
Date Posted: 05-Aug-2010 at 2:34pm
I have gotten this far now. I just need to figure out how to deal with these two problems I get

public void FetchMembers(MemberQuery q)

{

List<Expression<Func<Member, bool>>> expressions = new List<Expression<Func<Member, bool>>>();

if(q.SSN != string.Empty)

expressions.Add(PredicateBuilder.Make(typeof(Member), "SocSecNo", FilterOperator.IsEqualTo, q.SSN));

if(q.CardNo != 0)

expressions.Add(PredicateBuilder.Make(typeof(Member), "CardNo", FilterOperator.IsEqualTo, q.CardNo));

if(q.EmployeeNo != string.Empty)

expressions.Add(PredicateBuilder.Make(typeof(Member), "EmployeeNo", FilterOperator.IsEqualTo, q.EmployeeNo));

if(q.LastName != string.Empty)

expressions.Add(PredicateBuilder.Make(typeof(Member), "LastName", FilterOperator.IsEqualTo, q.LastName));

if(q.FirstName != string.Empty)

expressions.Add(PredicateBuilder.Make(typeof(Member), "FirstName", FilterOperator.IsEqualTo, q.FirstName));

var newExpr = PredicateBuilder.And(expressions.ToArray());

var query = PredicateBuilder.FilterQuery(entityManager.Members, newExpr);

var results = query.Execute<Member>();

results.ForEach(Members.Add);

}

I am getting two made errors they prefer to each add line.

Error 1 The best overloaded method match for 'System.Collections.Generic.List<System.Linq.Expressions.Expression<System.Func<DomainModel.Member,bool>>>.Add(System.Linq.Expressions.Expression<System.Func<DomainModel.Member,bool>>)' has some invalid arguments D:\Software Development\VS2010\TicketWare2010\TicketWare2010\Models\MemberViewModel.cs 67 18 TicketWare2010

 
Error 2 Argument 1: cannot convert from 'IdeaBlade.Linq.PredicateDescription' to 'System.Linq.Expressions.Expression<System.Func<DomainModel.Member,bool>>' D:\Software Development\VS2010\TicketWare2010\TicketWare2010\Models\MemberViewModel.cs 67 34 TicketWare2010



Replies:
Posted By: BillG
Date Posted: 06-Aug-2010 at 3:27pm
Any suggestions?


Posted By: jsobell
Date Posted: 08-Aug-2010 at 5:56am
Add all of the criteria but add an 'or' to each one checking for an empty string value, effectively creating a query that says
WHERE
     (SocSecNo={SSN} OR {SSN}="") AND (CardNo={CardNo} OR {CardNo}="") AND ...

The SQL query optimizer will exclude all of the subqueries where the variable is blank, so this doesn't affect performance.
The other advantage is that you can code this in a single statement instead of having lots of conditionals, so it becomes a cached queryplan at the SQL Server end.



Print Page | Close Window