New Posts New Posts RSS Feed: update to problem with PredicateBuilder
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

update to problem with PredicateBuilder

 Post Reply Post Reply
Author
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: update to problem with PredicateBuilder
    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
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 06-Aug-2010 at 3:27pm
Any suggestions?
Back to Top
jsobell View Drop Down
Groupie
Groupie
Avatar

Joined: 02-Apr-2009
Location: Australia
Posts: 80
Post Options Post Options   Quote jsobell Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down