Post

3 followers Follow
0
Avatar

What is the right way to perform query by multiple Ids.

What is the right way to perform query by multiple Ids.

For example get entity "ResourceLink" where WorkItem filed
equals "id-1" or "id-2"…or "id-n".

Like sql "in"
statement.

Thanks

 

Import from old forum Answered

Please sign in to leave a comment.

7 comments

0
Avatar

There are 2 ways to achieve this:

  1. Using where with multiple OR statements (i.e. WorkItem = id-1 OR WorkItem = id-2 .. etc).

  2. Using IN with query.

The second option will only work if you can get the list of Work Item IDs using a subquery

Clarizen Team 0 votes
Comment actions Permalink
0
Avatar

Can you give a sample code for the 2. option: "IN" statement
Thanks

Import from old forum 0 votes
Comment actions Permalink
0
Avatar

I'll give examples for the 2 options.

Option1: Let's assume you have 3 IDs of users, and you want to find all the tasks that are managed by one of those users. The query you'd want to perform is something like:

SELECT Name FROM Tasks Where Manager=1 OR Manager=2 OR Manager =3

 

If we translate it to C# it would look something like this:

  1. EntityQuery qry = new EntityQuery();
  2. qry.TypeName = "Task";
  3. qry.Fields = new string[] { "Name", "StartDate", "Manager" };
  4.  
  5. qry.Where = new Or
  6. {
  7.     Conditions = new[]
  8.     {
  9.         new Compare
  10.         {
  11.                 LeftExpression = new FieldExpression { FieldName = "Manager" },
  12.                 Operator = Operator.Equal,
  13.                 RightExpression = new ConstantExpression{Value =
  14.                     new EntityId {TypeName="User",Value="Id1" } }
  15.         },
  16.         new Compare
  17.         {
  18.                 LeftExpression = new FieldExpression { FieldName = "Manager" },
  19.                 Operator = Operator.Equal,
  20.                 RightExpression = new ConstantExpression{Value =
  21.                     new EntityId {TypeName="User",Value="Id2" } }
  22.         },
  23.         new Compare
  24.         {
  25.                 LeftExpression = new FieldExpression { FieldName = "Manager" },
  26.                 Operator = Operator.Equal,
  27.                 RightExpression = new ConstantExpression{Value =
  28.                     new EntityId {TypeName="User",Value="Id3" } }
  29.         }
  30.     }
  31. };
Clarizen Team 0 votes
Comment actions Permalink
0
Avatar

For the 2nd Option:

Let's assume you want to find the tasks that are managed by users who's name starts with "A".

The SQL would look something like this:

SELECT Name FROM Tasks Where Manager IN  (SELECT ID FROM Users Where Username LIKE "A%")

If we translate it to C# it would look something like this:

  1. EntityQuery qry = new EntityQuery();
  2. qry.TypeName = "Task";
  3. qry.Fields = new string[] { "Name", "StartDate", "Manager" };
  4.  
  5. qry.Where = new Compare
  6. {
  7.     LeftExpression = new FieldExpression { FieldName = "Manager" },
  8.     Operator = Operator.In,
  9.     RightExpression = new QueryExpression
  10.     {
  11.         Query = new EntityQuery
  12.         {
  13.             TypeName = "User",
  14.             Where = new Compare
  15.             {
  16.                 LeftExpression = new FieldExpression { FieldName = "Username" },
  17.                 Operator = Operator.BeginsWith,
  18.                 RightExpression = new ConstantExpression { Value = "A" }
  19.             }
  20.         }
  21.     }
  22. };
Clarizen Team 0 votes
Comment actions Permalink
0
Avatar

Hello,

 

Could you advice me please, how query like 

SELECT DisplayName FROM Users WHERE id in( SELECT ... ) will look like?

Which field should i specify for LeftExpression in this case?

 

(I need to select users which belongs to some set of groups)

 

Thank you

Oleg Savich 0 votes
Comment actions Permalink
0
Avatar

Hi Oleg. If you have the list of IDs and this is the only filter, why not use RetrieveMultipleMessage instead?

If you need to use a Query condition though, specify EntityIdExpression on the left expression (EntityIdExpression has no properties),

Equal on the operator and a _ConstantExpression _containing just the Id in the right expression

Sample:

Where =new Compare

             {

                     LeftExpression=new EntityIdExpression(),

                     Operator = Operator.Equal ,

                     RightExpression =new ConstantExpression(){Value=projectId.Value}

          }

Obviously, you have to create several of these and wrap them with an Or condition.

 

Hope this helps

Ophir Kenig 0 votes
Comment actions Permalink