Author |
Share Topic Topic Search Topic Options
|
bala
Groupie
Joined: 18-Aug-2010
Location: Brazil
Posts: 54
|
Post Options
Quote Reply
Topic: With Predicate 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
|
 |
sbelini
IdeaBlade
Joined: 13-Aug-2010
Location: Oakland
Posts: 786
|
Post Options
Quote Reply
Posted: 12-Nov-2010 at 3:25pm |
Bala,
You should use:
query = _em.PERSON.Where(checkP);
Regards,
sbelini.
|
 |
bala
Groupie
Joined: 18-Aug-2010
Location: Brazil
Posts: 54
|
Post Options
Quote Reply
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
|
 |
sbelini
IdeaBlade
Joined: 13-Aug-2010
Location: Oakland
Posts: 786
|
Post Options
Quote Reply
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.
|
 |
BillG
DevForce MVP
Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
|
Post Options
Quote Reply
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);
}
);
|
 |
bala
Groupie
Joined: 18-Aug-2010
Location: Brazil
Posts: 54
|
Post Options
Quote Reply
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
|
 |
Molinari
Groupie
Joined: 25-Aug-2010
Posts: 42
|
Post Options
Quote Reply
Posted: 25-Jan-2011 at 10:43am |
Hi BillG,
you tried to use navigation in PredicateBuilder ?
thanks
|
 |
BillG
DevForce MVP
Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
|
Post Options
Quote Reply
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);
}
}
});
|
 |
Molinari
Groupie
Joined: 25-Aug-2010
Posts: 42
|
Post Options
Quote Reply
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.
|
 |
BillG
DevForce MVP
Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
|
Post Options
Quote Reply
Posted: 25-Jan-2011 at 5:27pm |
I was not able to figure out how to do this. Sorry.
|
 |
Molinari
Groupie
Joined: 25-Aug-2010
Posts: 42
|
Post Options
Quote Reply
Posted: 26-Jan-2011 at 2:42am |
Ok... thanks
|
 |
sbelini
IdeaBlade
Joined: 13-Aug-2010
Location: Oakland
Posts: 786
|
Post Options
Quote Reply
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.
|
 |
Molinari
Groupie
Joined: 25-Aug-2010
Posts: 42
|
Post Options
Quote Reply
Posted: 26-Jan-2011 at 10:05am |
ok... thanks
|
 |
Molinari
Groupie
Joined: 25-Aug-2010
Posts: 42
|
Post Options
Quote Reply
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.
Edited by Molinari - 24-May-2011 at 10:38am
|
 |