Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# Enumerable.Last()抛出InvalidOperationException“;序列不包含匹配元素“;_C#_.net_Linq - Fatal编程技术网

C# Enumerable.Last()抛出InvalidOperationException“;序列不包含匹配元素“;

C# Enumerable.Last()抛出InvalidOperationException“;序列不包含匹配元素“;,c#,.net,linq,C#,.net,Linq,在预先排序的列表中我将找到满足条件的最后一个元素,例如int lastScore=List.last(x=>xxint?确定*/ }捕获(无效操作异常){ lastScore=null;/*如果为0,请按照建议查看LastOrDefault; 否则,使用哨兵/旗帜/等做出适当反应*/ } if(lastScore.HasValue){ /*找到满足条件的值*/ } 或者,如果无法在未找到的情况下丢弃案例,请考虑: int? lastScore; /* Using a Nullable<

在预先排序的
列表中
我将找到满足条件的最后一个元素,例如
int lastScore=List.last(x=>x<100)
。如果列表中没有满足此条件的元素,将抛出一个
InvalidOperationException
,并显示错误消息:
序列不包含匹配的元素。这在
列表中发生。首先(…)
也是如此

我甚至试图将
lastScore
设为空,但都没有用


捕获异常并手动将
lastScore
分配给
null
是否是唯一的出路?

如果没有匹配项,则使用
FirstOrDefault
LastOrDefault
获取
null
。这些方法将返回for类型的值。

我可能只是在最近的使用点捕获异常

这是因为
IEnumerable
上的
LastOrDefault/FirstOrDefault
将返回0(int
的默认值),这可能是一个“有效”值-这取决于实际上下文和定义的规则。虽然将序列转换为
IEnumerable
将允许前面的方法返回
null
,但这似乎需要做更多的工作

如果继续使用时需要使用
lastScore
,请考虑:

int? lastScore;  /* Using a Nullable<int> to be able to detect "not found" */
try {
   lastScore = list.Last(x => x < 100);  /* int -> int? OK */
} catch (InvalidOperationException) {
   lastScore = null; /* If 0 see LastOrDefault as suggested;
                        otherwise react appropriately with a sentinel/flag/etc */
}
if (lastScore.HasValue) {
   /* Found value meeting conditions */
}
try {
   var lastScore = list.Last(x => x < 100);
   /* Do something small/immediate that won't throw
      an InvalidOperationException, or wrap it in it's own catch */
   return lastScore * bonus;
} catch (InvalidOperationException) {
   /* Do something else entirely, lastScore is never available */
   return -1;
}
int?最后得分;/*使用可为Nullable来检测“未找到”*/
试一试{
lastScore=list.Last(x=>x<100);/*int->int?确定*/
}捕获(无效操作异常){
lastScore=null;/*如果为0,请按照建议查看LastOrDefault;
否则,使用哨兵/旗帜/等做出适当反应*/
}
if(lastScore.HasValue){
/*找到满足条件的值*/
}
或者,如果无法在未找到的情况下丢弃案例,请考虑:

int? lastScore;  /* Using a Nullable<int> to be able to detect "not found" */
try {
   lastScore = list.Last(x => x < 100);  /* int -> int? OK */
} catch (InvalidOperationException) {
   lastScore = null; /* If 0 see LastOrDefault as suggested;
                        otherwise react appropriately with a sentinel/flag/etc */
}
if (lastScore.HasValue) {
   /* Found value meeting conditions */
}
try {
   var lastScore = list.Last(x => x < 100);
   /* Do something small/immediate that won't throw
      an InvalidOperationException, or wrap it in it's own catch */
   return lastScore * bonus;
} catch (InvalidOperationException) {
   /* Do something else entirely, lastScore is never available */
   return -1;
}
试试看{
var lastScore=list.Last(x=>x<100);
/*做一些小的/即时的事情,不会让人失望
一个InvalidOperationException,或者将其包装在自己的捕获中*/
返回最后分数*奖金;
}捕获(无效操作异常){
/*完全做其他事情,lastScore永远不可用*/
返回-1;
}

欢迎来到SO!说到这里,我被你的问题弄糊涂了
List
不能是
List
的类型,因为
int
当然没有属性
Score
。请澄清。真的很抱歉!我想写
list.Last(x=>x<100)
。谢谢你指点。我也编辑了这个问题。在这种情况下(
IEnumerable
),它将返回0,而不是null。如果0是有效值,则这实际上是有问题的。一种解决方案是首先将序列转换为
IEnumerable
,尽管我可能只处理本例中的异常。True。那么,解决方案是什么呢?刚刚发现
firstOrDetail
LastOrDefault
返回值类型的默认值(例如
int
s的0)。对于
IEnumerable
,其中
T
是引用类型,它们返回
null