C# 通过LINQ中的另一个集合对集合进行分组和筛选
我的表格中有大量数据:C# 通过LINQ中的另一个集合对集合进行分组和筛选,c#,linq,grouping,C#,Linq,Grouping,我的表格中有大量数据: public class Permission { public string Url { get; set; } public string User { get; set; } public string PermissionLevel { get; set; } } Key: http://domain/project/1 Permission 1 (Url: http://domain/project/1/xyz/xyz)
public class Permission
{
public string Url { get; set; }
public string User { get; set; }
public string PermissionLevel { get; set; }
}
Key: http://domain/project/1
Permission 1 (Url: http://domain/project/1/xyz/xyz)
Permission 3 (Url: http://domain/project/1/xyzz/xqeqyz)
Permission 7 (Url: http://domain/project/1/xadadyz/xadadyz)
Key: http://domain/project/2
Permission 2 (Url: http://domain/project/2/adyz/xdyz)
Permission 4 (Url: http://domain/project/2/a2dyz/xdyz)
Permission 21(Url: http://domain/project/2/a22dyz/xdyz)
Key: http://domain/project/3
Permission 22 (Url: ...
第二个集合作为列表SiteCollections
。样本数据:
http://domain/project/1
http://domain/project/2
http://domain/project/3
http://domain/project/4
http://domain/project/5
现在,我的目标是通过用户
筛选此列表权限
。所以这给了我
var FilteredAndGrouped = data.Where(u => u.User == @"domain\username")
在这之后,我想按url的一部分对结果进行分组。每个权限url都以列表SiteCollections中的一个url开头
如果你是从
var filteredAndGrouped = data.Where(d => d.User == @"domain\User")
.GroupBy (d => SiteCollections.Any (sc => sc.StartsWith(d.Url)))
这将导致一个键为true/false的分组,并且只有与列表SiteCollections
中完全相同的URL在键为true的分组中。
我想要的是一个分组,其中键是列表sitecolections
中的url,权限url需要以分组的键开始。它不需要等于它
最后,我需要以下表格中的结果:
public class Permission
{
public string Url { get; set; }
public string User { get; set; }
public string PermissionLevel { get; set; }
}
Key: http://domain/project/1
Permission 1 (Url: http://domain/project/1/xyz/xyz)
Permission 3 (Url: http://domain/project/1/xyzz/xqeqyz)
Permission 7 (Url: http://domain/project/1/xadadyz/xadadyz)
Key: http://domain/project/2
Permission 2 (Url: http://domain/project/2/adyz/xdyz)
Permission 4 (Url: http://domain/project/2/a2dyz/xdyz)
Permission 21(Url: http://domain/project/2/a22dyz/xdyz)
Key: http://domain/project/3
Permission 22 (Url: ...
结果可能包含
null
键,这意味着url在SiteCollections
中不存在。请注意:我有点愚蠢。我现在只盯着FirstOrDefault()
思考:我为什么不使用它?工作起来很有魅力。谢谢你有时候,你看不见树木,看不见森林:)