我们在后台代码里面操作Entity的时候,基本上是这样写的:

/* 创建者:菜刀居士的博客

 * 创建日期:2014年07月5号

 */

 namespace Net.CRM.Entity

{

    using System;

    using Microsoft.Xrm.Sdk;

    /// <summary>

    /// 基本模式---Entity

    /// </summary>

    public class EntityDemo

    {

        public void Run(Entity entity)

        {

            if (IsNotNull(entity,"new_int"))

            {

                //获取int类型的字段的值

                int new_int = entity.GetAttributeValue<int>("new_int");

            }

            if (IsNotNull(entity, "new_string"))

            {

                //获取string类型的字段的值

                string new_string = entity.GetAttributeValue<string>("new_string");

            }

            if (IsNotNull(entity, "new_float"))

            {

                //获取float类型的字段的值

                float new_float = entity.GetAttributeValue<float>("new_float");

            }

            if (IsNotNull(entity, "new_money"))

            {

                //获取Money(货币)类型的字段的值

                decimal new_money = entity.GetAttributeValue<Money>("new_money").Value;

            }

            if (IsNotNull(entity, "new_picklist"))

            {

                //获取OptionSetValue(下拉框)类型的字段的值

                int new_picklist = entity.GetAttributeValue<OptionSetValue>("new_picklist").Value;

            }

            if (IsNotNull(entity, "new_lookup"))

            {

                //获取EntityReference(引用字段)类型的字段的值

                EntityReference new_lookup = entity.GetAttributeValue<EntityReference>("new_lookup");

            }

        }

        /// <summary>

        /// 推断实体的某个字段是否为空

        /// </summary>

        /// <param name="entity">实体</param>

        /// <param name="name">字段名称</param>

        public bool IsNotNull(Entity entity,string name)

        {

            return entity.Contains(name) && entity.Attributes[name] != null;

        }

    }

}

 

这样写。是正确的,可是。非常繁琐,以下是高速的写法:

 /* 创建者:菜刀居士的博客

 * 创建日期:2014年07月5号

 */

namespace Net.CRM.Entity

{

    using System;

    using Microsoft.Xrm.Sdk;

    /// <summary>

    /// 高速开发---Entity

    /// </summary>

    public class EntityQuickDemo

    {

        public void Run(Entity entity)

        {

            if (entity.IsNotNull("new_int"))

            {

                //获取int类型的字段的值

                int new_int = entity.ToInt("new_int");

            }

            if (entity.IsNotNull("new_string"))

            {

                //获取string类型的字段的值

                string new_string = entity.ToString("new_string");

            }

            if (entity.IsNotNull("new_float"))

            {

                //获取float类型的字段的值

                float new_float = entity.ToFloat("new_float");

            }

            if (entity.IsNotNull("new_money"))

            {

                //获取Money(货币)类型的字段的值

                decimal new_money = entity.ToMoney("new_money");

            }

            if (entity.IsNotNull("new_picklist"))

            {

                //获取OptionSetValue(下拉框)类型的字段的值

                int new_picklist = entity.ToOpInt("new_picklist");

            }

            if (entity.IsNotNull("new_lookup"))

            {

                //获取EntityReference(引用字段)类型的字段的值

                EntityReference new_lookup = entity.ToEr("new_lookup");

            }

        }

    }

    /// <summary>

    /// 扩展方法

    /// </summary>

    public static class EntityFunction

    {

        /// <summary>

        /// Int

        /// </summary>

        public static int ToInt(this Entity entity, string name)

        {

           return entity.GetAttributeValue<int>(name);

        }

        /// <summary>

        /// string

        /// </summary>

        public static string ToString(this Entity entity, string name)

        {

            return entity.GetAttributeValue<string>(name);

        }

        /// <summary>

        /// float

        /// </summary>

        public static float ToFloat(this Entity entity, string name)

        {

            return entity.GetAttributeValue<float>(name);

        }

        /// <summary>

        /// Money

        /// </summary>

        public static decimal ToMoney(this Entity entity, string name)

        {

            return entity.GetAttributeValue<Money>(name).Value;

        }

        /// <summary>

        /// OptionSetValue

        /// </summary>

        public static int ToOpInt(this Entity entity, string name)

        {

            return entity.GetAttributeValue<OptionSetValue>(name).Value;

        }

        /// <summary>

        /// EntityReference

        /// </summary>

        public static EntityReference ToEr(this Entity entity, string name)

        {

            return entity.GetAttributeValue<EntityReference>(name);

        }

        public static T GetAttributeValue<T>(this Entity entity, string name)

        {

            if (entity.IsNotNull(name))

            {

                return entity.GetAttributeValue<T>(name);

            }

            return default(T);

        }

        /// <summary>

        /// 推断实体的某个字段是否为空

        /// </summary>

        /// <param name="entity">实体</param>

        /// <param name="name">字段名称</param>

        public static bool IsNotNull(this Entity entity, string name)

        {

            return entity.Contains(name) && entity.Attributes[name] != null;

        }

    }

}