crm高速开发之QueryExpression
/* 创建者:菜刀居士的博客
* 创建日期:2014年07月06号
*/
namespace Net.CRM.OrganizationService
{
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Collections.Generic;
/// <summary>
/// QueryExpression
/// </summary>
public class QueryExpressionDemo
{
/// <summary>
/// 基本模式,构造查询--QueryExpression
/// </summary>
public void CreateQueryExpression()
{
QueryExpression query = new QueryExpression();
//查询的实体名称
query.EntityName = "new_accountproduct";
//查询实体的属性集合
query.ColumnSet = new ColumnSet("new_product","new_name","new_price");
//查询条件
query.Criteria.AddCondition(new ConditionExpression("statecode",ConditionOperator.Equal,0));
query.Criteria.AddCondition(new ConditionExpression("new_name", ConditionOperator.Like, "水果" + "%"));
List<Guid> list = new List<Guid>();
list.Add(new Guid("27BB1B31-09BB-E311-B590-D89D671782D3"));
list.Add(new Guid("E8F8D412-31BB-E311-B590-D89D671782D3"));
query.Criteria.AddCondition(new ConditionExpression("new_product",ConditionOperator.In,list.ToArray()));
query.Criteria.AddCondition(new ConditionExpression("new_name", ConditionOperator.NotNull));
query.Criteria.AddCondition(new ConditionExpression("new_price", ConditionOperator.Null));
}
/// <summary>
/// 高速模式,构造查询--QueryExpression
/// </summary>
public void CreateQuickQueryExpression()
{
QueryExpression query = "new_accountproduct".ToQuery("new_product", "new_name", "new_price");
query.AddEqual("statecode", 0);
query.AddLike("new_name", "水果" + "%");
List<Guid> list = new List<Guid>();
list.Add(new Guid("27BB1B31-09BB-E311-B590-D89D671782D3"));
list.Add(new Guid("E8F8D412-31BB-E311-B590-D89D671782D3"));
query.AddIn("new_product", list.ToArray());
query.AddNotNull("new_name");
query.AddNull("new_price");
}
}
public static class ExtensionFunction
{
public static QueryExpression ToQuery(this string entityName)
{
QueryExpression query = new QueryExpression();
query.EntityName = entityName;
return query;
}
public static QueryExpression ToQuery(this string entityName,params string[] Attrs)
{
QueryExpression query = new QueryExpression();
query.EntityName = entityName;
query.ColumnSet = new ColumnSet(Attrs);
return query;
}
public static void AddEqual<T>(this QueryExpression query,string name,T value)
{
query.Criteria.AddCondition(new ConditionExpression(name,ConditionOperator.Equal,value));
}
public static void AddLike(this QueryExpression query, string name,string value)
{
query.Criteria.AddCondition(new ConditionExpression(name,ConditionOperator.Like,value));
}
public static void AddIn<T>(this QueryExpression query, string name,T[] values)
{
query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.In, values));
}
public static void AddNotNull(this QueryExpression query,params string[] attrs)
{
if (attrs != null && attrs.Length > 0)
{
foreach(string name in attrs)
{
query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.NotNull));
}
}
}
public static void AddNull(this QueryExpression query, params string[] attrs)
{
if (attrs != null && attrs.Length > 0)
{
foreach (string name in attrs)
{
query.Criteria.AddCondition(new ConditionExpression(name, ConditionOperator.Null));
}
}
}
}
}
对照: