C# caml查询如何返回null?

C# caml查询如何返回null?,c#,visual-studio-2010,sharepoint-2010,caml,C#,Visual Studio 2010,Sharepoint 2010,Caml,我的代码中有一个caml查询,用于返回SharePoint列表中的某些项目。我添加了IF语句,以防IF查询找不到任何匹配项或返回NULL SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery() { Query = @"<Where>

我的代码中有一个caml查询,用于返回SharePoint列表中的某些项目。我添加了IF语句,以防IF查询找不到任何匹配项或返回NULL

                   SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                   {
                       Query = @"<Where>
                                   <Eq>
                                     <FieldRef Name='Department'/>
                                     <Value Type='Text'>"+Department+"</Value>
                                   </Eq>
                                 </Where>"
                    });

                    foreach (SPListItem item in Items)
                    {
                        if (item != null)
                        {
                            item["Name"]="abcd";
                            item.Update();
                        }
                        else
                        {
                            newListItem["Name"] = "xyz";
                            newListItem.Update();
                        }
                    }
SPListItemCollection Items=RiskAssessment.GetItems(新的SPQuery()
{
查询=@“
“+部门+”
"
});
foreach(项目中的SPListItem项目)
{
如果(项!=null)
{
项目[“名称”]=“abcd”;
item.Update();
}
其他的
{
newListItem[“名称”]=“xyz”;
newListItem.Update();
}
}
如果在列表中找不到Department,则不会转到ELSE语句

试试看{
         try{
    SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery() 
                        { 
                            Query = @"<Where> 
                                             <Eq> 
                                                <FieldRef Name='Department'/> 
                                                <Value Type='Text'>"+Department+"</Value>                           </Eq></Where>" 
                        }); 
                         if(Items != null){
                        foreach (SPListItem item in Items) 
                        { 
                            if (item != null) 
                            { 
                                item["Name"]="abcd"; 
                                item.Update(); 
                            } 
                            else 
                            { 
                                newListItem["Name"] = "xyz"; 
                                newListItem.Update(); 
                            } 
                        } 

                    }

        }
        Catch(Exception exc){
        //Do something with your exception here
        }
SPListItemCollection Items=RiskAssessment.GetItems(新的SPQuery() { 查询=@“ “+部门+” }); 如果(项!=null){ foreach(项目中的SPListItem项目) { 如果(项!=null) { 项目[“名称”]=“abcd”; item.Update(); } 其他的 { newListItem[“名称”]=“xyz”; newListItem.Update(); } } } } 捕获(异常exc){ //在这里处理你的例外情况 }
我解决了它。查询将只返回您输入的部门。所以foreach语句将只包含该部门中的项。所以我没有找到其他不匹配的项目。就像:

                       SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                        {
                            Query = @"<Where>
                                         <Eq>
                                            <FieldRef Name='Department'/>
                                            <Value Type='Text'>" + Department + "</Value></Eq></Where>"
                        });

                        if (Items.Count==0)
                        {
                           item["Name"]="abcd"; 
                            item.Update(); 
                        }
                        else
                        {
                            foreach (SPListItem item in Items)
                            {
                                if (item != null)
                                {
                                   item["Name"]="abcd"; 
                            item.Update(); 
                                }
                            }
                        }
SPListItemCollection Items=RiskAssessment.GetItems(新的SPQuery()
{
查询=@“
“+部门+”
});
if(Items.Count==0)
{
项目[“名称”]=“abcd”;
item.Update();
}
其他的
{
foreach(项目中的SPListItem项目)
{
如果(项!=null)
{
项目[“名称”]=“abcd”;
item.Update();
}
}
}

使用项目计数来确定是否有您首先选择的项目。如果您有count,则使用for each to循环。

如果您的splistitemcollection=NULL,for循环将失败。试着抓住它我想你可能会出错你想做什么?对我来说,我不知道为什么要检查for循环中的item是否为null,因为我认为如果它在splistitemcollection中,它应该始终是something。也许你的意思是检查标题是否为空。if(item!=null)可能应该是if(string.isNullOrEmpty(item[Title].ToString())if(string.isNullOrEmpty(item[SPBuiltInField.Title].ToString())