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;

        }

    }

}

CRM Entity 之Money转string int类型等的更多相关文章

  1. char,String,int类型互转

    1.ascci码对应转换 字符,对应的ascii(其实是UTF-16)码:     char c='a'; int k=(int) c;    结果k=97 数字,对应的字符:     int k=9 ...

  2. golang 中string和int类型相互转换

    总结了golang中字符串和各种int类型之间的相互转换方式: string转成int: int, err := strconv.Atoi(string)string转成int64: int64, e ...

  3. java中将string类型转int类型或者将string类型转long类型方法

    将字串 String 转换成整数 int 两种方法: 1).int i = Integer.parseInt("111"); 或 i = Integer.parseInt([Str ...

  4. Java int类型与String类型互转

    String类型转换为int类型 参考:https://blog.csdn.net/qq_35995940/article/details/78433404?locationNum=5&fps ...

  5. Java不同类型字符转换String/int/Float/////

    1.int & String int i=5678;String s=""; int->String: s=i+"";或 s=String.val ...

  6. 如何将int整型转换成String字符串类型

    自动类型转换适用于兼容类型之间从小范围到大范围数据的转换. nt转换成String int i = 10; int b=1: System.out.pritnln(a + b); 里面靠近字符串,所以 ...

  7. java将int类型的变量转化成String类型的

    第一种方法:String的valueOf方法,int i=5;String s=String.valueOf(i);第二种方法,直接在int后面加一个空的字符串,因为在java里面,默认任务int类型 ...

  8. Item 30 用enum代替int常量类型枚举,string常量类型枚举

    1.用枚举类型替代int枚举类型和string枚举类型 public class Show {   // Int枚举类型   // public static final int APPLE_FUJI ...

  9. linq中将int类型转换为string类型,toString()报错

    今天同事在调试程序的时候,报了一个不寻常的错误, “LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 ...

随机推荐

  1. SQLite.dll混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。

    其他信息: V5.7.4.4 Can't find the System.Data.SQLite.dll more info : 混合模式程序集是针对"v2.0.50727"版的运 ...

  2. Prefabs实例化 ResourceMgr

    http://www.xiaobao1993.com/886.html 来源与小宝个人笔记[稍作修改] //使用  Prefabs/Resources/stone1 ResourceMgr.GetIn ...

  3. js类的几种写法

    我们常用的有以下几种方法来用JavaScript写一个“类”: 1. 构造函数(public属性和方法) 1: function Person(iName, iAge){ 2: this.name=i ...

  4. 关于ECharts Java类库的一个jquery插件

    在项目中开发图表功能时用到了Echars和一个关于Echars的java类库(http://git.oschina.net/free/ECharts).这个类库主要目的是方便在Java中构造EChar ...

  5. js 完美兼容浏览器的复制功能

    1,js结合swf的复制功能,完美兼容火狐,谷歌,360,ie8,使用示例:(ps:引入copy.swf比较重要,文件传送门 解压密码:http://www.bieanju.com/,为了防止360删 ...

  6. Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。

    Python urllib模块urlopen()与urlretrieve()的使用方法详解   1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...

  7. shell编程001

    1.shell中如何进行算术计算   A=1; B=2 (1)let C=$A+$B (2)C=$[$A+$B] (3)C=$(($A+$B)) (4)C=`expr $A + $B` (注意运算符前 ...

  8. 关于ASP.Net的一些概念 转载

    ①理解 .NET Platform Standard 作者:田园里的蟋蟀 http://www.cnblogs.com/xishuai/archive/2016/05/24/understand-do ...

  9. 前端中的SEO

    前端中的SEO: mate.title META标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). <Meta name="Keywords" ...

  10. return 和 echo 的小坑

    在写项目的时候,有好几次遇到过同样的问题,控制器里面返回的json在前台无显示,利用console.log()总是显示这样的现象 数据库操作成功,却没有返回值. 原因是在控制器返回的使用使用了 ret ...