Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# 值不能为null,paramName:LINQ中的值_C#_Asp.net Mvc_Linq - Fatal编程技术网

C# 值不能为null,paramName:LINQ中的值

C# 值不能为null,paramName:LINQ中的值,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我的linq where子句有一个问题。代码: public IEnumerable<SearchResult> Search(int? orderId, string customer, string car, int? type, int? status, string sortBy = null) { var records = Orders.Where(x => (x.Id == orderId || orderId == null) &

我的linq where子句有一个问题。代码:

public IEnumerable<SearchResult> Search(int? orderId, string customer, string car, int? type, int? status, string sortBy = null)
    {

        var records = Orders.Where(x => (x.Id == orderId || orderId == null) &&
                                        (string.Format("{0} {1}", x.Customer.Name, x.Customer.Surname).Contains(customer) || string.IsNullOrEmpty(customer)) &&
                                        (string.Format("{0} {1}", x.Car.Mark, x.Car.Model).Contains(car) || string.IsNullOrEmpty(car)) &&
                                        (x.OrderType.Id == type || type == null) &&
                                        (x.OrderStatus.Id == status || status == null));


    }
public IEnumerable搜索(int?orderId、string customer、string car、int?type、int?status、string sortBy=null)
{
var记录=订单。其中(x=>(x.Id==orderId | | orderId==null)&&
(string.Format(“{0}{1}”,x.Customer.Name,x.Customer.Name)。包含(Customer)| | string.IsNullOrEmpty(Customer))&&
(string.Format(“{0}{1}”,x.Car.Mark,x.Car.Model).包含(Car)| string.IsNullOrEmpty(Car))&&
(x.OrderType.Id==type | | type==null)&&
(x.OrderStatus.Id==status | | status==null));
}

问题在于医嘱ID、类型和状态。VS引发的异常类似于title:value不能为null,paramName:value。这很奇怪,因为当我开始搜索时,这些参数(orderId、类型、状态)在初始化时必须为null。

如果将null传递给
字符串。Contains
方法,它会抛出“
值不能为null。ParamName:Value.
”异常。
话虽如此,我怀疑
customer
car
参数为null。

如果将null传递给
字符串.Contains
方法,它会抛出“
值不能为null。ParamName:Value.
异常。
话虽如此,我怀疑
customer
car
参数为null。

Contains在字符串中使用时不喜欢null,并且假设您的所有参数都可以为null,您可以这样分离查询:

public IEnumerable<SearchResult> Search(int? orderId, string customer, string car, int? type, int? status, string sortBy = null)
{

  var records = Orders;

  if(orderId!=null)
  {
    records=records.Where(x=>x.Id == orderId);
  }

  if(!string.IsNullOrWhiteSpace(customer))
  {
     records=records.Where(string.Format("{0} {1}", x.Customer.Name, x.Customer.Surname).Contains(customer));
  }

.. and so on

}
public IEnumerable搜索(int?orderId、string customer、string car、int?type、int?status、string sortBy=null)
{
var记录=订单;
if(orderId!=null)
{
记录=记录。其中(x=>x.Id==orderId);
}
如果(!string.IsNullOrWhiteSpace(客户))
{
records=records.Where(string.Format(“{0}{1}”,x.Customer.Name,x.Customer.姓氏)。包含(Customer));
}
等等
}

我认为这样做可以提高性能,因为询问每个参数可以防止迭代每个项目,询问其值在字符串中使用时,Contains不喜欢NULL,并且假设您的所有参数都可以为NULL,您可以像这样分离查询:

public IEnumerable<SearchResult> Search(int? orderId, string customer, string car, int? type, int? status, string sortBy = null)
{

  var records = Orders;

  if(orderId!=null)
  {
    records=records.Where(x=>x.Id == orderId);
  }

  if(!string.IsNullOrWhiteSpace(customer))
  {
     records=records.Where(string.Format("{0} {1}", x.Customer.Name, x.Customer.Surname).Contains(customer));
  }

.. and so on

}
public IEnumerable搜索(int?orderId、string customer、string car、int?type、int?status、string sortBy=null)
{
var记录=订单;
if(orderId!=null)
{
记录=记录。其中(x=>x.Id==orderId);
}
如果(!string.IsNullOrWhiteSpace(客户))
{
records=records.Where(string.Format(“{0}{1}”,x.Customer.Name,x.Customer.姓氏)。包含(Customer));
}
等等
}

我认为您可以通过这一点获得性能上的胜利,因为询问每个参数可以防止迭代每个项目询问其值

“初始化时必须为null”您的意思是什么
订单的类型
出于好奇?具体来说,它是否继承自
IQueryable
?无论如何,这个错误几乎可以肯定,因为您正在将一个空的
客户
汽车
传递给
.Contains()
。您可以通过先选中
IsNullOrEmpty()
来避免这种情况,从而缩短对
Contains()
的调用,因为我想要所有的记录。我建议你将所有的
null
检查移动到每个
|
表达式的左侧。是的,它可以工作,非常感谢“初始化时必须为null”你是什么意思
订单的类型出于好奇?具体来说,它是否继承自
IQueryable
?无论如何,这个错误几乎可以肯定,因为您正在将一个空的
客户
汽车
传递给
.Contains()
。您可以通过先选中
IsNullOrEmpty()
来避免这种情况,从而缩短对
Contains()
的调用,因为那个么我想要所有的记录。我建议你们将所有的
null
检查移动到每个
|
表达式的左侧。是的,它是有效的,非常感谢集合中的一个项目可以为null,然后Contains(null)将是一个有效的搜索,这不适用于字符串中的字符集合中的一个项可以为null,然后Contains(null)将是有效的搜索,这不适用于字符串中的字符