Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过LINQ中的另一个集合对集合进行分组和筛选_C#_Linq_Grouping - Fatal编程技术网

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()
思考:我为什么不使用它?工作起来很有魅力。谢谢你有时候,你看不见树木,看不见森林:)