小编ba_*_*end的帖子

在C#中搜索嵌套列表<>的最快方法

我有一个List <>,其中包含另一个List <>

我需要查找最内层列表中的任何项目中是否存在给定值.如果找到匹配,我需要该特定项目并返回.

我这样做如下所示:

InnerList inner = null;
foreach(TopList in topListItems)
{
    inner = asn.Owners.Find(x => x.GuestId == guestId);
    if(inner != null)
         break;
}

//item found if inner is not null
//else item absent in the inner list

Any other alternate way that may run faster than this?
Run Code Online (Sandbox Code Playgroud)

编辑: 一些更正:我只需要看看内部列表是否有一个具有特定值的项目.如果是,那么我需要返回具有匹配项的顶级项目.我猜逻辑是一样的.

c# nested list

5
推荐指数
2
解决办法
9257
查看次数

C#:更好的编码方式?

我有一个代码块来处理我的应用程序中的异常,它使用if/else块来获取消息内容.
我的代码如下:

// define variable to hold exceptions...
var exceptionMessage = new StringBuilder();  
// based on the exception type...  
if (expType == typeof(EntityValidationException))  
{  
    // append the relevant message to the text...  
    exceptionMessage.Append(exception.InnerException.Message);  
}  
else if (expType == typeof(ValidationException))  
{  
    // This is the type of error generated when entities are validated  
    var validationException = (ValidationException)exception;  
    exceptionMessage.Append(validationException.InnerException.Message);  
}  
else if (expType == typeof(DomainSecurityException))  
{  
    // These are security breaches  
    var domainSecurityException = (DomainSecurityException)exception;  
    exceptionMessage.Append(domainSecurityException.InnerException.Message);  
}  
else if (expType == typeof(DomainInternalMessageException)) …
Run Code Online (Sandbox Code Playgroud)

c# if-statement

4
推荐指数
1
解决办法
441
查看次数

标签 统计

c# ×2

if-statement ×1

list ×1

nested ×1