无法隐式转换类型或处理null [英] Cannot Implicitly convert types or handle nulls

查看:93
本文介绍了无法隐式转换类型或处理null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建类以及将十进制,日期,布尔值甚至某些整数值从Visual Studio 2010 C#Silverlight应用程序中的域服务传递给类时,我遇到了一些困难.网上有几篇关于此主题的文章,但是它们目前超出了我对C#和WCF的了解,并且我找不到单个示例可以传递字符串或整数以外的任何内容.在大多数情况下,我可以正常工作,但我的一些整数值需要后缀.Value,而有些则不需要.我不确定为什么.

我尝试创建和使用的类定义为:

要填充的类

I am having some difficulty with creating a class and passing decimal, date, boolean and even some integer values to the class from my Domain Service in a Visual Studio 2010 C# Silverlight application. There are several articles on this subject on the net but they are currently above my knowledge of C# and WCF and I cannot find a single example that passes anything other than a string or an integer. For the most part I have that working but some of my integer values require a suffix of .Value and some do not. I am not sure why.

The class I am trying to create and use is defined as:

Class to be populated

public class pmEmployee
    {
        [Key]
        public int id { get; set; }
        [DataMember]
        public string member_num { get; set; }
        public string last_name { get; set; }
        public string first_name { get; set; }
        public string middle_name_or_initial { get; set; }
        public string address1  {get; set; }
        public string address2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public DateTime birth_date { get; set; }
        public DateTime hire_date { get; set; }
        public string home_phone { get; set; }
        public string cell_phone { get; set; }
        public string work_phone_and_extension { get; set; }
        public string work_phone_extension { get; set; }
        public string SSN { get; set; }
        public string clock_num { get; set; }
        public int local_id { get; set; }
        public int site_id { get; set; }        
        public string email_address { get; set; }
        public int member_status_id { get; set; }
        public int member_type_id { get; set; }
        public int member_job_title_id { get; set; }      
        public string Marital  { get; set; }
        public string sex { get; set; }
        public string notes { get; set; }
        public int race_type_id  { get; set; }
        public string urnumber { get; set; }
        public string bad_address_ind { get; set; }
        public int dues_rate_id { get; set; }
        public decimal cope_contribution { get; set; }
        public bool grievance_exists_ind { get; set; }
        public decimal overage_amt {get; set; }
    }




我要破解的Iqueryable域服务是:




The Domain Service Iqueryable I am hacking away at is:

public IQueryable GetEmployee(int EMPID)
   {
     return from emp in ObjectContext.tblEmployees
       where emp.id == EMPID 
       select new pmEmployee()
      {
        id = emp.id,
        member_num = emp.member_num == null ? null :  emp.member_num,
        last_name = emp.last_name == null ? null : emp.last_name,
        first_name = emp.first_name == null ? null : emp.first_name,
        middle_name_or_initial  = emp.middle_name_or_initial == null ?  null : emp.middle_name_or_initial,

       address1 = emp.address1 == null ? null : emp.address1,
       address2 = emp.address2 == null ? null : emp.address2,
       city = emp.city == null ? null : emp.city,
       state = emp.state == null ? null : emp.state,
       zip = emp.zip == null ? null : emp.zip,
       cell_phone = emp.cell_phone == null ? null : emp.cell_phone,
       local_id = emp.local_id.Value,
       home_phone = emp.home_phone == null ? null : emp.home_phone,
       work_phone_and_extension = emp.work_phone_and_extension == null ? null : emp.work_phone_and_extension,

      work_phone_extension = emp.work_phone_extension  == null ? null : emp.work_phone_extension,

      SSN = emp.SSN == null ? null :  emp.SSN,
      clock_num = emp.clock_num == null ? null : emp.clock_num,                      
      site_id = emp.site_id.Value,
      email_address = emp.email_address == null ? null : emp.email_address,                      
      member_status_id = emp.member_status_id.Value,
      member_type_id = emp.member_type_id.Value,
  //  member_job_title_id =  emp.member_job_title_id.Value,  
      Marital = emp.Marital  == null ? null : emp.Marital,
      sex = emp.sex  == null ? null : emp.sex,
      notes = emp.notes == null ? null : emp.notes,
//Bad race_type_id  = emp.race_type_id.Value,
      urnumber = emp.urnumber  == null ? null : emp.urnumber,
      bad_address_ind = emp.bad_address_ind  == null ? null : emp.bad_address_ind,

//    birth_date = (DateTime) emp.birth_date.Value,
//   overage_amt = (Decimal) emp.overage_amt
//      dues_rate_id = emp.dues_rate_id.Value,
                     
                       
                   };
                   }



我不确定为什么某些被注释掉的整数会引起问题,但是当我将它们添加到网格中时,系统会崩溃,永远不会降落.我尝试将(int)放在整数值的前面,这似乎无济于事.当有数据时,date birth_date有效,但是当db中的字段为null时,我得到一个隐式null异常.我不知道该如何处理小数或布尔值.例如,如果我将鼠标悬停在overage_amt(原始表中的十进制值)上,则会收到消息:

无法将类型十进制?"隐式转换为十进制.是否存在显式转换(您是否缺少演员表)?

请不要将我指向WCF上的Microsoft文章,我已经阅读了所有文章,并且不理解它们,我正在寻找有关变量的帮助.我真的在寻找可以告诉我如何处理null并将数据从域服务传递到xaml上的控件的人,而无需在VS2010中进行轰炸.在此先多谢.

谢谢,

拉里

[修改:添加了前置标记]



I am not sure why some of the integers which are commented out cause problems but when I add them bound to a grid the system goes off to never never land. I have tried putting (int) in front of the integer calues and that does not seem to help. The date birth_date works when there is data but I get an implict null exception when the field in the db is null. I have no idea what to do with decimals or booleans. For instance if I hold the mouse over overage_amt which is a decimal value in the original table I get the message:

Cannot implicitly convert type ''decimal?'' to decimal. An explicit conversion exists (are you missing a cast)?

Please don''t point me to microsoft articles on WCF I have read them all and do not understand them I am looking for help with my variables. I am really looking for someone to tell me how to handle nulls and pass the data from the domain service to the controls on the xaml without bombing out in VS2010. many thanks in advance.

Thanks,

Larry

[Modified: added pre tags]

推荐答案

似乎您有时想要空值.当您得到空值时,事情可能会爆发.使用可为null的类型来处理此问题(例如,"decimal?"而不是"decimal").通常,不能为内置类型和结构类型分配空值.但是,如果在类型后面加上问号,则使其成为可为空的值类型,这意味着您可以为其分配null.您可以使用可为空的类型实例的HasValue属性确定它是否为空,并使用可为空的类型实例的Value属性获取值(当它不为null时).如果您尝试获取为null的值,则会抛出异常.
Seems like you sometimes want null values. When you get null values is likely when things bomb out. Use nullable types to handle this (e.g., "decimal?" instead of "decimal"). Normally, built-in types and struct types cannot be assigned a value of null. However, if you put a question mark after the type, that makes it a nullable value type, which means you can assign null to it. You can use the HasValue property of a nullable type instance to determine if it is null or not, and the Value property of a nullable type instance to get the value (when it is not null). If you try to get the value when it is null, that will throw an exception.


Larry,

首先,请先阅读下一部分代码&问题陈述我会问你一个问题.下面的代码和类似的代码行有什么需求?

Hi Larry,

First of all, before reading your next part of the code & problem statement I will ask you one question. What is the need of the below code and the similar kind of code lines?

member_num = emp.member_num == null ? null :  emp.member_num,




无需检查null&如果为null,则设置为null,否则为该值?它的重要部分是什么?这将增加将其检查为null的更多处理时间.另外,它将调用setter方法来设置值.

只需执行以下操作就足够了:




There is no need to check null & if it is null set null else the value? What is the significant part of it? This will increase more processing time for checking it as null. Also it will call the setter method to set the value.

Just do the following and that will be enough:

member_num = emp.member_num;



如果为null,则将其他值设置为null.希望这些信息对以后编写代码有帮助.如果有帮助,请标记为答案".



If it is null, will set as null else the value. Hope this information will help you in future while writing code. If this helps you, please "Mark As Answer".


Larry,

为了解决这个问题,您可以编写以下代码:

Hi Larry,

To solve this you can write the following code:

overage_amt = emp.overage_amt == null ? 0 : emp.overage_amt;
dues_rate_id = emp.dues_rate_id == null ? null : emp.dues_rate_id.Value;



另外,您可以使用可以处理null值的可为空的数据类型.看看第一个答案.这样可以解决您的问题.

约会时间? dateTime = null;
dateTime =新的DateTime();
dateTime = null;

在上面的代码中,它将永远不会出现任何错误.祝你一切顺利.编码愉快.干杯...:thumbsup:



Also, you can use nullable datatypes which can handle the null value. Have a look into the first answer. That will solve your problem in that case.

DateTime? dateTime = null;
dateTime = new DateTime();
dateTime = null;

In the above code, it will never through any error. Wish you all the best. Happy Coding. Cheers... :thumbsup:


这篇关于无法隐式转换类型或处理null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆