C# 使用不带循环的LINQ查找部分字符串匹配
问题:我正在搜索自定义标识对象中的用户角色集合。这些角色有一个连接到它们的部门,因此该角色不仅是“角色”,而且具有“部门:角色”的模式 由于角色是在不知道“部门”部分的对象中定义的,因此在构建授权列表时需要忽略此部分有效地,我在每个“部门:角色”项目中寻找“:角色”。 我正在使用LINQ来做这件事,并让它工作(发布在下面),但如果可能的话,我想通过删除C# 使用不带循环的LINQ查找部分字符串匹配,c#,string,linq,C#,String,Linq,问题:我正在搜索自定义标识对象中的用户角色集合。这些角色有一个连接到它们的部门,因此该角色不仅是“角色”,而且具有“部门:角色”的模式 由于角色是在不知道“部门”部分的对象中定义的,因此在构建授权列表时需要忽略此部分有效地,我在每个“部门:角色”项目中寻找“:角色”。 我正在使用LINQ来做这件事,并让它工作(发布在下面),但如果可能的话,我想通过删除foreach循环来简化它。我已经在网上搜索了几个小时,尝试了许多不同的解决方案。最接近我想要完成的两件事似乎是和。也许答案就在这里面,我只是不去
foreach
循环来简化它。我已经在网上搜索了几个小时,尝试了许多不同的解决方案。最接近我想要完成的两件事似乎是和。也许答案就在这里面,我只是不去摸索
提前感谢您的帮助/建议
我的代码:
protected static void AddObjectAuthorizationRules() {
//Code that gets current user context...
string[] pDefinedRoles = new string[] { "Developer", "Admin", "User" };
List<string> _createRoles = ProcessAuthorizationRoles(pDefinedRoles, pIdentity);
//Object authorization code that uses _createRoles...
}
private List<string> ProcessAuthorizationRoles(string[] pDefinedRoles, CustomIdentityClass pIdentity) {
List<string> _allowRoles = new List<string>();
foreach (var _role in pDefinedRoles) {
var partial = string.Format(":{0}", _role);
string[] tmp = (from r in pIdentity.Roles
where r.Contains(partial)
select r).ToArray();
foreach (string found in tmp) {
_allowRoles.Add(found);
}
}
return _allowRoles;
}
受保护的静态void AddObjectAuthorizationRules(){
//获取当前用户上下文的代码。。。
string[]pDefinedRoles=新字符串[]{“开发者”、“管理员”、“用户”};
列表_createRoles=ProcessAuthorizationRoles(pDefinedRoles,pIdentity);
//使用_createRoles的对象授权代码。。。
}
私有列表ProcessAuthorizationRoles(字符串[]pDefinedRoles,CustomIdentityClass pIdentity){
列表_allowRoles=新列表();
foreach(pDefinedRoles中的var_角色){
var partial=string.Format(“:{0}”,_角色);
字符串[]tmp=(来自pIdentity.Roles中的r
其中r.包含(部分)
选择r).ToArray();
foreach(在tmp中找到的字符串){
_allowRoles.Add(已找到);
}
}
返回允许孔;
}
我想我可以为您提供一个返回语句,但我没有“CustomIdentityClass”的引用,所以我无法为您测试
private List<string> ProcessAuthorizationRoles(string[] pDefinedRoles, CustomIdentityClass pIdentity)
{
return (from role in (from r in pDefinedRoles
select new
{
Partial = string.Format(":{0}", r)
})
from r in pIdentity.Roles
where r.Contains(role.Partial)
select r).ToList();
}
私有列表ProcessAuthorizationRoles(字符串[]pDefinedRoles,CustomIdentityClass pIdentity)
{
返回(来自中的角色)(来自pDefinedRoles中的r
选择新的
{
Partial=string.Format(“:{0}”,r)
})
来自pidr中的entity.Roles
其中r.Contains(role.Partial)
选择r.ToList();
}
我想我可以为您提供一个返回语句,但我没有“CustomIdentityClass”的引用,所以我无法为您测试
private List<string> ProcessAuthorizationRoles(string[] pDefinedRoles, CustomIdentityClass pIdentity)
{
return (from role in (from r in pDefinedRoles
select new
{
Partial = string.Format(":{0}", r)
})
from r in pIdentity.Roles
where r.Contains(role.Partial)
select r).ToList();
}
私有列表ProcessAuthorizationRoles(字符串[]pDefinedRoles,CustomIdentityClass pIdentity)
{
返回(来自中的角色)(来自pDefinedRoles中的r
选择新的
{
Partial=string.Format(“:{0}”,r)
})
来自pidr中的entity.Roles
其中r.Contains(role.Partial)
选择r.ToList();
}
我想我可以为您提供一个返回语句,但我没有“CustomIdentityClass”的引用,所以我无法为您测试
private List<string> ProcessAuthorizationRoles(string[] pDefinedRoles, CustomIdentityClass pIdentity)
{
return (from role in (from r in pDefinedRoles
select new
{
Partial = string.Format(":{0}", r)
})
from r in pIdentity.Roles
where r.Contains(role.Partial)
select r).ToList();
}
私有列表ProcessAuthorizationRoles(字符串[]pDefinedRoles,CustomIdentityClass pIdentity)
{
返回(来自中的角色)(来自pDefinedRoles中的r
选择新的
{
Partial=string.Format(“:{0}”,r)
})
来自pidr中的entity.Roles
其中r.Contains(role.Partial)
选择r.ToList();
}
我想我可以为您提供一个返回语句,但我没有“CustomIdentityClass”的引用,所以我无法为您测试
private List<string> ProcessAuthorizationRoles(string[] pDefinedRoles, CustomIdentityClass pIdentity)
{
return (from role in (from r in pDefinedRoles
select new
{
Partial = string.Format(":{0}", r)
})
from r in pIdentity.Roles
where r.Contains(role.Partial)
select r).ToList();
}
私有列表ProcessAuthorizationRoles(字符串[]pDefinedRoles,CustomIdentityClass pIdentity)
{
返回(来自中的角色)(来自pDefinedRoles中的r
选择新的
{
Partial=string.Format(“:{0}”,r)
})
来自pidr中的entity.Roles
其中r.Contains(role.Partial)
选择r.ToList();
}
如果您可以使用列表而不是数组,您可以尝试以下方法
private List<string> ProcessAuthorizationRoles(List<string> pDefinedRoles, CustomIdentityClass pIdentity)
{
return pIdentity.Roles.FindAll(x => pDefinedRoles.Exists(y => x.Contains(string.format(":{0}", y))));
}
私有列表ProcessAuthorizationRoles(列表pDefinedRoles、CustomIdentityClass pIdentity)
{
返回pIdentity.Roles.FindAll(x=>pDefinedRoles.Exists(y=>x.Contains(string.format(“:{0}”,y)));
}
如果您可以使用列表而不是数组,您可以尝试以下方法
private List<string> ProcessAuthorizationRoles(List<string> pDefinedRoles, CustomIdentityClass pIdentity)
{
return pIdentity.Roles.FindAll(x => pDefinedRoles.Exists(y => x.Contains(string.format(":{0}", y))));
}
私有列表ProcessAuthorizationRoles(列表pDefinedRoles、CustomIdentityClass pIdentity)
{
返回pIdentity.Roles.FindAll(x=>pDefinedRoles.Exists(y=>x.Contains(string.format(“:{0}”,y)));
}
如果您可以使用列表而不是数组,您可以尝试以下方法
private List<string> ProcessAuthorizationRoles(List<string> pDefinedRoles, CustomIdentityClass pIdentity)
{
return pIdentity.Roles.FindAll(x => pDefinedRoles.Exists(y => x.Contains(string.format(":{0}", y))));
}
私有列表ProcessAuthorizationRoles(列表pDefinedRoles、CustomIdentityClass pIdentity)
{
返回pIdentity.Roles.FindAll(x=>pDefinedRoles.Exists(y=>x.Contains(string.format(“:{0}”,y)));
}
如果您可以使用列表而不是数组,您可以尝试以下方法
private List<string> ProcessAuthorizationRoles(List<string> pDefinedRoles, CustomIdentityClass pIdentity)
{
return pIdentity.Roles.FindAll(x => pDefinedRoles.Exists(y => x.Contains(string.format(":{0}", y))));
}
私有列表ProcessAuthorizationRoles(列表pDefinedRoles、CustomIdentityClass pIdentity)
{
返回pIdentity.Roles.FindAll(x=>pDefinedRoles.Exists(y=>x.Contains(string.format(“:{0}”,y)));
}
谢谢@SpikeX。;我所看到的CustomIdentityClass中的主要内容是.Roles属性,它只是一个列表。您的答案是可行的,但就性能而言,这与@derbal213的建议相比如何?我对LINQ的内部运作还不够熟悉,不知道其中一个是否会比另一个更好。如果有帮助的话,可以在pDefinedRoles参数中使用列表而不是数组。从性能上看,列表和数组大致相同(有一些区别,但除非您处理的是大数据,否则通常不重要)。通常来说,LINQ更“漂亮”,但性能不如LINQ