Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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/2/.net/21.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#?_C#_.net_Linq - Fatal编程技术网

如何处理列表中不匹配的大小写。首先是c#?

如何处理列表中不匹配的大小写。首先是c#?,c#,.net,linq,C#,.net,Linq,在函数中,如果没有匹配项,如何处理该情况?目前它只是崩溃了 MySPListItem firstItem = itemCollection.First(item => !item.isFolder); if (firstItem != null) { TreeNode firstNode = GetNodeByListItem(my_treeview.Nodes, firstItem, ReportObject); if (firstNode != null) {

在函数中,如果没有匹配项,如何处理该情况?目前它只是崩溃了

MySPListItem firstItem = itemCollection.First(item => !item.isFolder);
if (firstItem != null)
{
    TreeNode firstNode = GetNodeByListItem(my_treeview.Nodes, firstItem, ReportObject);
    if (firstNode != null)
    {
        ReportObject.log("Selecting the first PDF");
        selectPDF(my_treeview, firstNode, queryStr_param);
    }
}
错误消息:

序列不包含匹配元素
Stacktrace:at System.Linq.Enumerable.First[TSource](IEnumerable
1源,Func
2 谓词)

从您链接到的网站中引用:

与指定谓词定义的条件匹配的第一个元素(如果找到);否则,类型T的默认值为

这描述了返回值。因此,当未找到匹配项时,将返回类型
T
默认值,这意味着引用类型为
null
,值类型为
0
false
&co


因此,在调用代码中,只需检查此默认值,就可以了:-)。您不能只使用返回的值,因为如果您使用的是引用类型,这可能会导致
NullReferenceException

检查结果是否为
null

   if (result == null)
   {
      Console.WriteLine("Not found");
   }

有一个清晰的示例演示了如果项目被找到/未找到该怎么办。它不应该引发异常,您需要处理它返回默认值(t)的情况。您可能会得到一个NullReferenceException,因为您没有处理它返回null的情况。例如:

IEnumerable<Cars> cars = GetCars();
Car car = cars.Find(c => c.Model == "Astra");
if(car != null) 
{
   // You found a car!
}

当您在find上执行
null
检查时,您不需要在谓词中执行检查。线路

    foundItem = itemCollection.Find(item => item.item.ID == PDFID);
如果
item
null
(您是否在集合中插入了
null
项?)或
项,则可能引发异常。item
null
(您确定它始终存在吗?)

你可以做:

foundItem = itemCollection.Find(item => item != null &&
                                        item.item != null && 
                                        item.item.ID == PDFID);
更健谈,但您不会得到
NullReferenceException

编辑好吧,你改变了你的问题。现在您首先执行
。如果未找到任何内容,则
First
方法将引发异常。改为使用
FirstOrDefault
,它将为类返回
null
,或为
结构返回默认值

    foundItem = itemCollection.FirstOrDefault(item => item != null &&
                                              item.item != null && 
                                              item.item.ID == PDFID);
首先替换为:


好的,如果没有第一个是异常的

try
{
    var first = enumerable.First();
}
catch (InvalidOperationException)
{
    // Oops, that was exceptional.
}
如果您预计在某些有效的情况下可能没有第一个

var first = enumerable.FirstOrDefault();
if (first == default(someType)) // null for reference types.
{
    // Ok, I need to deal with that.
}

如果没有匹配项,它将返回该类型的默认值。。(
null
对于类,是结构的默认值)。放入您的代码,我们将能够告诉您错误发生的位置。作为替代方案,为什么不使用LINQ的FirstOrDefault(item=>item.item.ID==PDFID)您会遇到什么异常?我添加了异常消息。我还将其更改为.First,虽然我也使用.Find,但我想知道解决此问题的正确方法。First和Find的行为不同。如果没有匹配项,First将抛出异常。请参阅@Dann comment。我添加了异常消息。我不认为这是因为该项为空。请参阅我的更新。您更改了方法(它是
Find
,但现在是
First
),这是一个比公认的更好的答案。简明,并考虑引发异常的原因。
default(someType)
null
之间有什么区别?@omega
default(T)
返回类型的默认值。对于引用类型,这总是
null
,因此,在您的情况下,它们是可互换的。如果您想让代码处理值类型(结构和原语),那么
default(T)
提供了一个通用的解决方案。当然,如果您知道类型,您可以只使用一个等于默认值的文本,这是编译器为您所做的。
try
{
    var first = enumerable.First();
}
catch (InvalidOperationException)
{
    // Oops, that was exceptional.
}
var first = enumerable.FirstOrDefault();
if (first == default(someType)) // null for reference types.
{
    // Ok, I need to deal with that.
}