问题描述
Microsoft提供例如像我抄在下面为一个QueryExpression创建多个条件之一。有没有方法来构建一个QueryExpression,让你可以动态地处理数目不详的条件是什么?在微软下面的例子中,他们使用条件1,条件2等等......再次声明,我不知道是否有创造一个更加可重复使用QueryExpression可以处理可变数目的条件的方法。我知道整件事可以在LINQ中做,但我专门试图确定它是否能够与QueryExpression来完成。
//创建查询表达式,并设置联系的实体。
QueryExpression查询=新QueryExpression();
query.EntityName =联系;
//创建其中第一个名称等于乔的条件。
ConditionExpression条件1 =新ConditionExpression();
condition1.AttributeName =名字;
condition1.Operator = ConditionOperator.Equal;
condition1.Values =新的字符串[] {乔};
//创建其中第一个名称等于约翰的另一个条件。
ConditionExpression条件2 =新ConditionExpression();
条件2 .AttributeName =名字;
条件2 .Operator = ConditionOperator.Equal;
条件2 .Values =新的字符串[] {约翰};
所以,你可以以编程建立QueryExpressions,这可能有助于简化对象的创建。唯一的问题我会提高,就是你可能会发现你的查询是如此的不同,很难建立通用的功能,支持他们。
在这里无论如何都是一个。简单的例子,它应该有希望让你开始
公共静态QueryExpression BuildQueryExpression(字符串的entityName,columnSet columnSet,logicalOperator logicalOperator,列表与LT; ConditionExpression> ;条件)
{
QueryExpression查询=新QueryExpression(的entityName);
query.ColumnSet = columnSet;
query.Criteria =新FilterExpression(logicalOperator);
conditions.ForEach(C => query.Criteria.AddCondition(C));
返回查询;
}
用法:
QueryExpression查询= BuildQueryExpression(接触,新ColumnSet(真),LogicalOperator.And,新的List< ConditionExpression>()
{
新ConditionExpression(名字,ConditionOperator.Equal,詹姆斯),
新ConditionExpression(姓氏,ConditionOperator.Equal,木),
});
Microsoft provides example like the one I have copied below for creating multiple conditions for a QueryExpression. Is there way to structure a QueryExpression so that you could dynamically handle a unknown number of conditions? In Microsofts example below they use condition1, condition2 and so on... Again I'm wondering if there's a way to create a more reusable QueryExpression that can handle a variable number of conditions. I know the whole thing could be done in LINQ but I'm specifically trying to determine if it could be done with QueryExpression.
// Create the query expression and set the entity to contact.
QueryExpression query = new QueryExpression();
query.EntityName = "contact";
// Create a condition where the first name equals Joe.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "firstname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new string[] { "Joe" };
// Create another condition where the first name equals John.
ConditionExpression condition2 = new ConditionExpression();
condition2 .AttributeName = "firstname";
condition2 .Operator = ConditionOperator.Equal;
condition2 .Values = new string[] { "John" };
So you could programmatically build QueryExpressions, which might help to streamline object creation. The only issue I would raise, is that you may find your queries are so different it is difficult to create generic functions to support them all.
In any case here is a simple example which should hopefully get you started.
public static QueryExpression BuildQueryExpression(String entityName, ColumnSet columnSet, LogicalOperator logicalOperator, List<ConditionExpression> conditions)
{
QueryExpression query = new QueryExpression(entityName);
query.ColumnSet = columnSet;
query.Criteria = new FilterExpression(logicalOperator);
conditions.ForEach(c => query.Criteria.AddCondition(c));
return query;
}
Usage:
QueryExpression query = BuildQueryExpression("contact", new ColumnSet(true), LogicalOperator.And, new List<ConditionExpression>()
{
new ConditionExpression("firstname", ConditionOperator.Equal, "James" ),
new ConditionExpression("lastname", ConditionOperator.Equal, "Wood" ),
});
这篇关于有没有方法来构建一个QueryExpression,让你可以动态地处理数目不详的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!