C# 内联if语句不工作

C# 内联if语句不工作,c#,jquery,asp.net-mvc-2,jqgrid,if-statement,C#,Jquery,Asp.net Mvc 2,Jqgrid,If Statement,出于某种原因,当我向这段代码中添加三元if语句时,会抛出一个NullPointerException。我不太清楚为什么…有什么想法吗?这是jqGrid返回Json数据的方法 var gridModel = from entity in vendorList.AsQueryable() select new { VendorId = "<a href='/Admin/DetailsPlan/" + entity.

出于某种原因,当我向这段代码中添加三元if语句时,会抛出一个NullPointerException。我不太清楚为什么…有什么想法吗?这是jqGrid返回Json数据的方法

var gridModel = from entity in vendorList.AsQueryable()
            select new
            {
                VendorId = "<a href='/Admin/DetailsPlan/" + entity.VendorId + "'><img src='/Images/next_icon_sm.png' class='icon' alt='View Vendor' /></a>",
                VendorNm = entity.VendorNm,
                Phone = (entity.Phone.Length < 5) ? String.Format("{0:(###) ###-####}", Convert.ToInt64(entity.Phone)) : entity.Phone,
                City = entity.City,
                State = entity.LkState.StateAbbr
            };

在那个位置不能有三元if语句吗?

一个问题,entity.Phone是否为空?如果是这样,那就是原因

旁注:我不得不说,这是一种储存电话号码的奇怪方式

更新

问题在于entity.Phone.Length部分。如果Phone为空,则无法访问其长度属性。。。因此出现了错误。因此,您需要添加一个空测试。比如:

Phone = ((entity.Phone != null) && (entity.Phone.Length < 5)) ? String.Format("{0:(###) ###-####}", Convert.ToInt64(entity.Phone)) : entity.Phone

这样的话,如果它是空的,你只是在发出一个空值。

一个问题,entity.Phone是空的吗?如果是这样,那就是原因

var gridModel = from entity in vendorList.AsQueryable()
    let unformattedPhone = entity.Phone??string.Empty
    select new
    {
        VendorId = "<a href='/Admin/DetailsPlan/" + entity.VendorId + "'><img src='/Images/next_icon_sm.png' class='icon' alt='View Vendor' /></a>",
        VendorNm = entity.VendorNm,
        Phone = (unformattedPhone.Length < 5) ? String.Format("{0:(###) ###-####}", Convert.ToInt64(unformattedPhone)) : unformattedPhone,
        City = entity.City,
        State = entity.LkState.StateAbbr
    };
旁注:我不得不说,这是一种储存电话号码的奇怪方式

更新

问题在于entity.Phone.Length部分。如果Phone为空,则无法访问其长度属性。。。因此出现了错误。因此,您需要添加一个空测试。比如:

Phone = ((entity.Phone != null) && (entity.Phone.Length < 5)) ? String.Format("{0:(###) ###-####}", Convert.ToInt64(entity.Phone)) : entity.Phone
这样,如果它为null,则只会发出一个null值

var gridModel = from entity in vendorList.AsQueryable()
    let unformattedPhone = entity.Phone??string.Empty
    select new
    {
        VendorId = "<a href='/Admin/DetailsPlan/" + entity.VendorId + "'><img src='/Images/next_icon_sm.png' class='icon' alt='View Vendor' /></a>",
        VendorNm = entity.VendorNm,
        Phone = (unformattedPhone.Length < 5) ? String.Format("{0:(###) ###-####}", Convert.ToInt64(unformattedPhone)) : unformattedPhone,
        City = entity.City,
        State = entity.LkState.StateAbbr
    };
这可能会解决你的问题


这可能会解决你的问题。

一元if语句是什么?@Peter:例如i++;而OP显然对此没有异议。我认为我们的好医生要找的词是三元的。更正.?:被称为条件运算符。它实际上是三元的,不是一元的。什么是一元if语句?@Peter:例如i++;而OP显然对此没有异议。我认为我们的好医生要找的词是三元的。更正.?:被称为条件运算符。它实际上是三元的,不是一元的。在某些情况下,它是空的。但是,如果我有String.Formatentity。。。。或者,如果我只是显示手机…没有错误..但两者的组合使其不显示。在某些情况下,它是空的。但是,如果我有String.Formatentity。。。。或者,如果我只是显示电话…没有错误..但两者的组合使其不显示。结果是entity.phone.Length<5-在某些情况下,entity.phone为空。但是,您的解决方案看起来很有趣-行let unformattedPhone=entity.Phone??string.empty the double??方法检查该值是否为空。如果不为NULL,则返回值,否则返回字符后面的字符串:结果是entity.phone.Length<5-在某些情况下,entity.phone为空。但是,您的解决方案看起来很有趣-行let unformattedPhone=entity.Phone??string.empty the double??方法检查该值是否为空。如果不为NULL,则返回值,否则返回字符后面的字符串: