最近在寫 QueryExpression 的時候用到了 ConditionOperator.In,在這裏簡單 Mark 一下它的用法。
1 List<Guid> listIds = new List<Guid>();//定義一個list 2 Guid g1 = Guid.NewGuid(); 3 Guid g2 = Guid.NewGuid(); 4 listIds.Add(g1); 5 listIds.Add(g2); 6 7 QueryExpression q = new QueryExpression("account"); 8 q.Criteria.AddCondition("accountid", ConditionOperator.In, listIds);//寫法1 9 q.Criteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.In, listIds));//寫法2 10 q.Criteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.In, new Guid[]{g1, g2}));//寫法3 11 q.Criteria.AddCondition("accountid", ConditionOperator.In, listIds.ConverAll(i=>(object)i).ToArray());//寫法4 12 q.Criteria.AddCondition("accountid", ConditionOperator.In, new object[]{g1, g2});//寫法5 13 q.Criteria.AddCondition("accountid", ConditionOperator.In, g1, g2);//寫法6
以上寫法中,寫法1會報錯,報錯信息如下:
Error: Condition for attribute 'account.accountid': expected argument(s) of type 'System.Guid' but received 'System.Collections.Generic.List
請改用其他幾種寫法即可。