看到一个老代码里的方法,是判断两个string 数组是否存在相同的元素:
快一百行代码了。。。。。
public bool HasRole(string[] roleList)
{
bool result;
string[] roleListStored, roleListInput;
string itemStoredToLower;
result = false;
roleListStored = this.roleList;
roleListInput = roleList;
if ((roleListStored != null) &&
(roleListInput != null))
{
foreach (string itemStored in roleListStored)
{
if (itemStored != null)
{
itemStoredToLower = itemStored.ToLower();
foreach (string itemInput in roleListInput)
{
if ((itemInput != null) &&
(itemInput.ToLower() == itemStoredToLower))
{
result = true;
break;
}
}
if (result)
{
break;
}
}
}
}
return result;
}
用Lamda表达式 简化语法例子,3行就行了:
string[] roleLis = new string[] { "18","22","95"};
string[] roleLisInput = new string[] { "1", "22","8","5" };
var s = roleLisInput.Count(n => string.IsNullOrEmpty(n) == false && n.ToLower()== roleLis.FirstOrDefault(n2 => n2 == n.ToLower()))>0;
或者
var exists = roleLisInput.Count(n => string.IsNullOrEmpty(n) == false && roleLis.Any(n2 => n2 == n.ToLower()))>0;
原文:https://www.cnblogs.com/wgscd/p/13679754.html