Domanda

I have a huge set of data in this form:

public class Permission
{
    public string Url { get; set; }
    public string User { get; set; }
    public string PermissionLevel { get; set; }
}

And a second collection as a List<string> SiteCollections. Sample data:

http://domain/project/1
http://domain/project/2
http://domain/project/3
http://domain/project/4
http://domain/project/5

Now my target is to filter this List<Permission> Permissions by User. So this gives me

var FilteredAndGrouped = data.Where(u => u.User == @"domain\username")

After this I want to group this result by part of its url. Every Permissions url is starts with one of the urls in the List SiteCollections.

If have started out with

var filteredAndGrouped = data.Where(d => d.User == @"domain\User")
                             .GroupBy (d => SiteCollections.Any (sc => sc.StartsWith(d.Url)))

This results in a grouping with the keys true/false and only the urls which are excatly as in the List SiteCollections are in the grouping with the key true. What I want is a grouping with the keys being the url in the list SiteCollections and the Permissions Url needs to start with the key of the grouping. It does not need to be equals to it.

In the end I need a result in this form:

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: ...
È stato utile?

Soluzione

var groupped = permissions
    .GroupBy(x => SiteCollections.FirstOrDefault(u => x.Url.StartsWith(u)))
    .Select(x => new { Key = x.Key, Permissions = x.ToArray() })
    .ToArray();

Result may contain null key, that means that url doesn't exist in SiteCollections.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top