Print Page | Close Window

EntityQuery, with "dynamic" where clause

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=2725
Printed Date: 26-Apr-2026 at 10:46pm


Topic: EntityQuery, with "dynamic" where clause
Posted By: Randar
Subject: EntityQuery, with "dynamic" where clause
Date 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?



Replies:
Posted By: ting
Date 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.



Posted By: sbelini
Date 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 http://drc.ideablade.com/xwiki/bin/view/Documentation/dynamic-queries - DevForce Resource Center .
 
Silvio.


Posted By: Randar
Date 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. ;-)



Print Page | Close Window