码迷,mamicode.com
首页 > 其他好文 > 详细

crm快速开发之QueryExpression

时间:2014-07-08 21:35:31      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:crm快速开发之queryexpress   crm自定义开发   queryexpression   crm开发   

/* 创建者:菜刀居士的博客
 * 创建日期: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));
                }
            }
        }
    }


}

对比:

bubuko.com,布布扣

 

 

 

bubuko.com,布布扣

 

crm快速开发之QueryExpression,布布扣,bubuko.com

crm快速开发之QueryExpression

标签:crm快速开发之queryexpress   crm自定义开发   queryexpression   crm开发   

原文地址:http://blog.csdn.net/y_f123/article/details/37497635

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!