Gua*_*apo 1 c# class list .net-4.0 ignore-case
我有以下示例:
public class Commands
{
public int ID { get; set; }
public List<string> Alias { get; set; }
}
public class UserAccess
{
public int AccessID { get; set; }
// other stuff not needed for the question
public List<Commands> AllowedCommands { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想在UserAccess上实现一种返回命令ID的方法,如果列表中没有找到别名,则返回NULL,请参阅下面我说的一个脏例子 HasCommand:
public class UserAccess
{
public ID { get; set; }
// other stuff not needed for the question
public List<Commands> AllowedCommands { get; set; }
public Commands HasCommand(string cmd)
{
foreach (Commands item in this.AllowedCommands)
{
if (item.Alias.Find(x => string.Equals(x, cmd, StringComparison.OrdinalIgnoreCase)) != null)
return item;
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是运行或实现HasCommand方法的最有效方法是什么?
或者有更好的方法将其实现到UserAccess中吗?
可以缩短一点点
public Commands HasCommand(string cmd)
{
return AllowedCommands.FirstOrDefault(c => c.Alias.Contains(cmd, StringComparer.OrdinalIgnoreCase));
}
Run Code Online (Sandbox Code Playgroud)
但它几乎是一回事.