With Predicate
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=2301
Printed Date: 01-Apr-2025 at 6:58pm
Topic: With Predicate
Posted By: bala
Subject: With Predicate
Date Posted: 12-Nov-2010 at 11:08am
Hi I have to search from 4 field at one time. Like I have ID, Email, Phone,Name. I am not sure user want to search with which field. User may choose any field for search or choose more than one or all.
I thought its best to use predicate but I am not able to sort out it..
Expression<Func<PERSON, bool>> p1, p3,p5,checkP; if (pID!= "") p1 = p => p.ID.Contains(pID); if (pEmail!= "") p2 = p => p.Email.Contains(pEmail); if (pPhone!= "") p3 = p => p.CPF_Phone.Contains(pPhone); if (pName!= "") p4 = p => p.Name.Contains(pName);
checkP = PredicateBuilder.Or(p1,p2,p3,p4); but P1,p2,p3,p4 showing unassigned variable... then I have to execute this query like query = _em.PERSON.Where((op => (op.));// Here i want to use result of checkp query.ExecuteAsync(op2 => {.....});
Where i am wrong... Help Appriciated.. Thanks
|
Replies:
Posted By: sbelini
Date Posted: 12-Nov-2010 at 3:25pm
Bala,
You should use:
query = _em.PERSON.Where(checkP);
Regards,
sbelini.
|
Posted By: bala
Date Posted: 16-Nov-2010 at 7:58am
Hi
Thanks very much your line is working perfectly but
When i checked it is value null then its giving error
if (pID!= "") p1 = p => p.ID.Contains(pID); ..... ...
checkP = PredicateBuilder.Or(p1,p2,p3,p4);
P1,p2,p3,p4 showing unassigned variable... How can I handle this value.. as i mention I am not sure what value will insert by user..
Thanks
|
Posted By: sbelini
Date Posted: 16-Nov-2010 at 8:39am
Hi Bala,
You need to check the string and determine if it complies with your set of rules before using the PredicateBuilder.
|
Posted By: BillG
Date Posted: 18-Nov-2010 at 7:37am
Here is how I do that. On my search screen I have multiple search abiltity, seach by name, status, office, id etc. I create a Query object which represents the various query fields and bind that object to the search fields. I then pass that query object to my getMembers for example and create my search criteria. It works just great. It doesn't matter how many search fields you have or which ones are used or not used.
public void GetMembers(MemberQO q)
{
List<PredicateDescription> predicateDescriptions = new List<PredicateDescription>();
PredicateDescription aPredicateDescription;
if (q.SSN != string.Empty)
{
aPredicateDescription = PredicateBuilder.Make(typeof(Member), "SocSecNo", FilterOperator.IsEqualTo,
q.SSN);
predicateDescriptions.Add(aPredicateDescription);
}
if (q.LastName != string.Empty)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "LastName", FilterOperator.StartsWith, q.LastName));
if (q.FirstName != string.Empty)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "FirstName", FilterOperator.StartsWith, q.FirstName));
if (q.EmployeeNo != string.Empty)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "EmployeeNumber", FilterOperator.IsEqualTo, q.EmployeeNo));
if (q.CardNo != null)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "CardNo", FilterOperator.IsEqualTo, q.CardNo));
if (q.Status != string.Empty)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "Status", FilterOperator.IsEqualTo, q.Status));
if (q.Office != string.Empty)
predicateDescriptions.Add( PredicateBuilder.Make(typeof(Member), "Office", FilterOperator.IsEqualTo, q.Office));
CompositePredicateDescription aCompositePredicateDescription = PredicateBuilder.And(predicateDescriptions.ToArray());
IEntityQuery query = (IEntityQuery)PredicateBuilder.FilterQuery(Mgr.Members, aCompositePredicateDescription);
Mgr.ExecuteQueryAsync< Member>((IEntityQuery<Member>)query,
(op) =>
{
var results = op.Results;
results.ForEach(Members.Add);
}
);
|
Posted By: bala
Date Posted: 18-Nov-2010 at 10:09am
Hi BillG, Great thanks I already worked on it but your solution is perfect I am applying now same..
Thank you very much
|
Posted By: Molinari
Date Posted: 25-Jan-2011 at 10:43am
Hi BillG,
you tried to use navigation in PredicateBuilder ?
thanks
|
Posted By: BillG
Date Posted: 25-Jan-2011 at 10:46am
Yes I have and it works great. Here is a sample of the call.
CompositePredicateDescription aCompositePredicateDescription = PredicateBuilder.And(predicateDescriptions.ToArray());
IQueryable query = PredicateBuilder.FilterQuery(Manager.JobSites.Include("Employer"), aCompositePredicateDescription);
Manager.ExecuteQueryAsync< JobSite>((IEntityQuery<JobSite>)query,
(op) =>
{
if (op.CompletedSuccessfully)
{
if (onSuccess != null)
onSuccess(op.Results);
}
else
{
if (onFail != null)
{
op.MarkErrorAsHandled();
onFail(op.Error);
}
}
});
|
Posted By: Molinari
Date Posted: 25-Jan-2011 at 11:54am
hi, BillG
I think not asked correctly. sorry.
this line =>IQueryable query = PredicateBuilder.FilterQuery(Manager.JobSites.Include("Employer"), aCompositePredicateDescription);
ok.. no problem. But I try using in predicate..
Example.
Member => Table
Employer => Table / Navigation
if (q.Employer.FirstName != string.Empty)
predicateDescriptions.Add(PredicateBuilder.Make(typeof(Member), "FirstName", FilterOperator.Contains, q.Employer.FirstName));
you tried to use.
|
Posted By: BillG
Date Posted: 25-Jan-2011 at 5:27pm
I was not able to figure out how to do this. Sorry.
|
Posted By: Molinari
Date Posted: 26-Jan-2011 at 2:42am
Posted By: sbelini
Date Posted: 26-Jan-2011 at 9:46am
Hi Bill and Molinari,
Navigation (or nested) properties in dynamic built queries are not available in DevForce2010 v6.0.7.0.
It will, however, be available on DevForce v6.0.8.0 coming up this week.
|
Posted By: Molinari
Date Posted: 26-Jan-2011 at 10:05am
Posted By: Molinari
Date Posted: 24-May-2011 at 10:37am
Hi belini,
I need more one dynamic.
example I have table PERSON and PERSON_EMAIL, then person has zero or more... emails. TABLE PERSON_EMAIL has FIELD MASTER = "TRUE" I think my query is
(
if (pQueryOperation.PERSON_EMAIL != null)
predicateDescriptions.Add(PredicateBuilder.Make(typeof(PERSON), "PERSON_EMAIL.EMAIL", FilterOperator.Contains, pQueryOperation.PERSON_EMAIL.First(f=>f.MARTER == "TRUE").EMAIL));
)
FIELD EMAIL = type string
I think it is not possible to make this version. Or is there another way?
thanks.
|
|