New Posts New Posts RSS Feed: EntityQuery, with "dynamic" where clause
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

EntityQuery, with "dynamic" where clause

 Post Reply Post Reply
Author
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Topic: EntityQuery, with "dynamic" where clause
    Posted: 27-May-2011 at 2:04pm
Well, thank you for just saving me hours of banging my head against the wall.

Worked perfectly, after I ported it over to VB.Net. ;-)
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2011 at 1:17pm
In case anybody is wondering what a possible solution would look like:
 
  var queriedID = 1;
  var queryType = typeof(Employee);
 
  var baseQuery = EntityQuery.Create(queryType, mgr);
  var properties = queryType.GetProperties();
  IEnumerable result;
  if (properties.Any(p => p.Name == "CompanyID")) {
    PredicateDescription predicate = PredicateBuilder.Make(queryType, "CompanyID", FilterOperator.IsEqualTo, queriedID);
    var query = baseQuery.Where(predicate);
    result = query.Execute();
  }
 
Detailed information about dynamic queries is available in the DevForce Resource Center.
 
Silvio.
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2011 at 12:13pm
Hi Randar - I saw you talked with Albert on the phone about this. Let us know if you need any more help with it.

Back to Top
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2011 at 8:40am
I've got some code I'm trying to port over.  A key part of it generates some dynamic sql like so:

strSQL = "Select * From " & SqlTableName & " Where CompanyID=" & CompanyID

I'm trying to figure out the best way to map this to an EntityQuery(Of T).  What I would like to do is:
1) Check if T has a specific property, called CompanyId.  This *should* be possible through reflection.
2) Once I've verified that this property exists, extend the EntityQuery to include this in the Where clause.  That, I'm not so sure how to do.

My current thinking is either:
a) Expose an interface on the underlying Entity called ICompany, that exposes the CompanyId.  I think I *should* be able to build predicate up once I have that and then attach that to the Where clause.
b) Give up...and change all the calling methods to include the company filter. 

Any thoughts?
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down