Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 接受int的null值的匿名数组_C#_Linq_Anonymous Arrays - Fatal编程技术网

C# 接受int的null值的匿名数组

C# 接受int的null值的匿名数组,c#,linq,anonymous-arrays,C#,Linq,Anonymous Arrays,查询订单模型 public class InquiryOrder { [Key] public int InquiryOrderId { get; set; } public string InquiryOrderName { get; set; } [ForeignKey("ProductType")] public int? ProductTypeId { get; set; } public virtual ProductType Pro

查询订单模型

public class InquiryOrder
{
    [Key]
    public int InquiryOrderId { get; set; }

    public string InquiryOrderName { get; set; }

    [ForeignKey("ProductType")]
    public int? ProductTypeId { get; set; }
    public virtual ProductType ProductType { get; set; }
}
查询订单控制器

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
    var result = from c in displayedInquiryOrders .AsEnumerable()
                  select new[] { 
                            Convert.ToString(c.InquiryOrderId),
                            c.InquiryOrderName,
                            c.ProductType.ProductTypeName,
                        };

如果
ProductTypeId
queryorder
表中有空值,我将从数组中的
c.ProductType.ProductTypeName
获取未设置为对象实例的对象引用。如何调整此数组,使其接受int的null值。谢谢

您可能需要使用三元运算符

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
var result = from c in displayedInquiryOrders .AsEnumerable()
              select new[] { 
                        Convert.ToString(c.InquiryOrderId),
                        c.InquiryOrderName,
                        c.ProductType != null ? c.ProductType.ProductTypeName : null, //if not null, then add the product type, otherwise add null
                    };
在C#6.0中,我认为您可以通过
?。

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
var result = from c in displayedInquiryOrders .AsEnumerable()
              select new[] { 
                        Convert.ToString(c.InquiryOrderId),
                        c.InquiryOrderName,
                        c.ProductType?.ProductTypeName
                    };

您可能需要使用三元运算符

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
var result = from c in displayedInquiryOrders .AsEnumerable()
              select new[] { 
                        Convert.ToString(c.InquiryOrderId),
                        c.InquiryOrderName,
                        c.ProductType != null ? c.ProductType.ProductTypeName : null, //if not null, then add the product type, otherwise add null
                    };
在C#6.0中,我认为您可以通过
?。

var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
var result = from c in displayedInquiryOrders .AsEnumerable()
              select new[] { 
                        Convert.ToString(c.InquiryOrderId),
                        c.InquiryOrderName,
                        c.ProductType?.ProductTypeName
                    };

如果
ProductTypeId
为空,这意味着实体框架不能延迟加载实体的产品类型,因为它不存在

这意味着当您调用
c.ProductType.ProductTypeName
c.ProductType
时,该值为null,并且在尝试获取
ProductTypeName
值时,您会得到
NullReferenceException

你可以用几种方法来解决这个问题。如果您使用的是最新版本的C#,则可以使用Null条件运算符(?)

如果没有,可以使用三元运算符执行相同的功能:

select new[] { 
    Convert.ToString(c.InquiryOrderId),
    c.InquiryOrderName,
    c.ProductType != null ? c.ProductTypeProductTypeName : string.Empty,
};

如果
ProductTypeId
为空,这意味着实体框架不能延迟加载实体的产品类型,因为它不存在

这意味着当您调用
c.ProductType.ProductTypeName
c.ProductType
时,该值为null,并且在尝试获取
ProductTypeName
值时,您会得到
NullReferenceException

你可以用几种方法来解决这个问题。如果您使用的是最新版本的C#,则可以使用Null条件运算符(?)

如果没有,可以使用三元运算符执行相同的功能:

select new[] { 
    Convert.ToString(c.InquiryOrderId),
    c.InquiryOrderName,
    c.ProductType != null ? c.ProductTypeProductTypeName : string.Empty,
};

是的,这很有效。谢谢你提供的宝贵信息!是的,这很有效。谢谢你提供的宝贵信息!是的,这很有效。因为史蒂夫把答案放在第一位,所以我把它标了出来。但是非常感谢你帮了我的忙是的这很有效。因为史蒂夫把答案放在第一位,所以我把它标了出来。但是非常感谢你对我的帮助。。