C# 可以跳过一行吗

C# 可以跳过一行吗,c#,outlook,C#,Outlook,在这里,我试图获取用户的outlook属性 如果一个用户拥有某些属性并检索相同的属性 如果另一个用户没有属性,则此代码终止 result.Properties["postalcode"][0]; 由于此属性不可用,某些用户索引的结果超出范围。 但是 此属性对该用户可用 excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0]; excelSheet.Cells[i, 10].Value =result.Prope

在这里,我试图获取用户的outlook属性

如果一个用户拥有某些属性并检索相同的属性

如果另一个用户没有属性,则此代码终止

result.Properties["postalcode"][0];
由于此属性不可用,某些用户索引的结果超出范围。 但是

此属性对该用户可用

    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
excelSheet.Cells[i, 10].Value =result.Properties["displayName"][0];

对于outlook属性中没有邮政编码的用户,是否可以读取displayname并跳过postalcode属性。

如果检查值的条件存在或不存在,则可以使用

if (result.Properties["postalcode"] != null && result.Properties["postalcode"].Count() > 0)
{
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
} 

您可以使用检查值的条件是否存在

if (result.Properties["postalcode"] != null && result.Properties["postalcode"].Count() > 0)
{
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
} 

您可以使用空条件:

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0];
诀窍是在
[“postalcode”]
之后加上问号。它是一种句法上的糖,它取代了

if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = null;
if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = "";
更新:如果需要返回某个特定值而不是null,则需要null coalecsing运算符。例如,如果您需要插入“”而不是null,它看起来像

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0]??"";
取代

if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = null;
if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = "";
还有一种情况:如果您在
excelSheet.Cells[i,9].value中有一个特定的值,则希望保留该值并使用一个单独的行程序。比你能耍这样的把戏好

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0]??excelSheet.Cells[i, 9].Value;

您可以使用空条件:

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0];
诀窍是在
[“postalcode”]
之后加上问号。它是一种句法上的糖,它取代了

if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = null;
if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = "";
更新:如果需要返回某个特定值而不是null,则需要null coalecsing运算符。例如,如果您需要插入“”而不是null,它看起来像

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0]??"";
取代

if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = null;
if(result.Properties["postalcode"]!=null)
    excelSheet.Cells[i, 9].Value = result.Properties["postalcode"][0];
else
    excelSheet.Cells[i, 9].Value = "";
还有一种情况:如果您在
excelSheet.Cells[i,9].value中有一个特定的值,则希望保留该值并使用一个单独的行程序。比你能耍这样的把戏好

excelSheet.Cells[i, 9].Value = result.Properties["postalcode"]?[0]??excelSheet.Cells[i, 9].Value;


您是否考虑过使用
if
条件?为什么不在获取值之前进行检查(如果存在)?如果没有,就不要实现你的逻辑。查看结果是否有与postalcode关联的属性。我不太熟悉Outlook内容,但如果
Properties[“postalcode”]
返回一个集合。可能类似于
excelSheet.Cells[i,9].Value=result.Properties[“postalcode”]?.FirstOrDefault()??字符串。空
您是否考虑过使用
if
条件?为什么不在获取值(如果存在)之前进行检查?如果没有,就不要实现你的逻辑。查看结果是否有与postalcode关联的属性。我不太熟悉Outlook内容,但如果
Properties[“postalcode”]
返回一个集合。可能类似于
excelSheet.Cells[i,9].Value=result.Properties[“postalcode”]?.FirstOrDefault()??字符串。空它不起作用,这个主意很好。我在找这种东西operator@Arya如果您需要条件(例如,[“postalcode”]如果为null,则将值设为零您可以使用null合并运算符执行此操作-请参阅更新。@如果null是值属性的有效值,例如,如果它是字符串,则MaxB不是。很好。但是如果用户不存在属性,则它将异常返回索引。这就是为什么我使用try-catch-block来处理此内容。@БММцццаааi将第一段代码与“”混合在一起。您的第一段代码无法编译。现在,在编辑之后,我知道您试图使用“”作为“列表[0]?.ToString()”,但您使用它是错误的,您无法访问“[0]”你尝试的方式。编辑:至少在我的低版本.Net测试程序中不是。在以后的版本中可能是这样。它不起作用,想法很好。我正在寻找这种东西operator@Arya如果您需要条件(例如,[“postalcode”]如果为null,则将值设为零您可以使用null合并运算符执行此操作-请参阅更新。@如果null是值属性的有效值,例如,如果它是字符串,则MaxB不是。很好。但是如果用户不存在属性,则它将异常返回索引。这就是为什么我使用try-catch-block来处理此内容。@БММцццаааi将第一段代码与“”混合在一起。您的第一段代码无法编译。现在,在编辑之后,我知道您试图使用“”作为“列表[0]?.ToString()”,但您使用它是错误的,您无法访问“[0]”您尝试的方式。编辑:至少不在我的低版本.Net测试程序中。在以后的版本中可能会这样。这是一个耗时的过程。每次都检查大量属性,不仅如此,您不能跳过wihtout检查,因为您需要if或?。但如果您可以共享更多信息,则可以找到最佳解决方案。例如,您的结果lts数据结构和预期结果…outlook属性的结果数据。一个用户可能有country或zip code属性,而另一个用户可能没有country属性。对于1000个用户,很难检查每个用户。一种方法是为每个属性状态设置try-catch块try-get值。如果返回null,则返回将在catch块中捕获,而下一个属性将不起作用这是一个耗时的过程。每次都会检查大量属性,不仅如此,您不能跳过wihtout检查,因为您需要if或?。但如果您可以共享更多信息,则可能会有最佳解决方案。如您的结果数据结构和预期结果…outlook pro中的结果数据perty。一个用户可能有country或zip code属性,而另一个用户可能没有country属性。当有1000个用户时,很难检查每个用户。一种方法是为每个属性状态尝试catch block,然后使用try get value。如果返回null,则它将在catch block中捕获,下一个属性将不起作用