CRM Entity 之Money转string int类型等

Money转string

左右都是string

//服务站地址

vehicleDetail["yt_servicestation_address"]=serviceStationCollEntity.Contains("yt_street")? serviceStationCollEntity["yt_street"] : null;

右侧Money 左侧string,需要转换成string

//工时单价

vehicleDetail["yt_workinghours"]=serviceStationCollEntity.Contains("yt_unit_price")? serviceStationCollEntity.GetAttributeValue<Money>("yt_unit_price").Value.ToString() : null;

 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;

        }

    }

}

这样写,是正确的,但是,很繁琐,下面是快速的写法:

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;

        }

    }

}
上一篇:Phone 3rd Recovery


下一篇:Ajax.ActionLink 用法