[Written] C # how to delete a value within an array, a set of values

===============================================

 2020/2 / 29_ first edit              ccb_warlock          

 

===============================================

Recently optimized a privilege checking, the system before each other in obtaining their permission system configuration, sso have to find the corresponding local authority to resolve once read files.

Although this design can realize the function, but this strategy is not economic to read again and again, especially in the case of high concurrency are more likely to become a performance bottleneck.

So I carried on this business optimization, and process optimization in a number of tests written for how to remove certain parameters within the array, I think the next record more elegant wording.

 


First talk about the next scene, a unified multi-system design authority by the sso control, then each need to ask "user rights in the existing system" after the user logs on to the sso other systems or "the user currently has the authority to operate this function" .

So sso need to get permission to verify current permissions information for that user and return by then, so in the service rights check, I first get permission once for each complete tree in the constructor based on the existing system type, behind If you find a missing before attempting to read a file, read directly without missing cache configuration.

 

Existing systems to enumerate records, assuming defined as follows:

public enum EnumSystemType
{
    NoMenu = 0 , // the system does not require permission or tree 
    app01 = . 1 ,
    App02 = 2 ,
    ......
}

 

Permissions for each system configuration is saved in separate files, such as tree preservation App01 complete authority in App01.json in.

Next is to obtain the contents of each file in accordance with the existing system and parsed into a tree permissions cached. The contents of the file at the time of acquisition, the enumeration does not need to get permission (NoMenu) need to skip find a logical file read. With me here is that linq function to implement the filtering logic of the enumeration values.

using System.Linq;

private Dictionary<string, List<PermissionNode>> GetPermissions()
{
    // all

    foreach (var fileName in Enum.GetNames(typeof(EnumSystemType))
       .Where(s => s != nameof(EnumSystemType.NoMenu))
       .ToArray())
    {
        // all 
    }

    // all 
}

The PS. Enum.GetNames The return value is an array of strings.

 

Filtered through a single parameter where it is easy to implement, and I suddenly thought of a problem, how to batch filtration enumerations it?

Check the check information, linq sure enough there is a solution, that is, by "Except".

 

Then I was to experiment with the above example, skip NoMenu and App01 time if I want to construct read, you can write:

using System.Linq;

private Dictionary<string, List<PermissionNode>> GetPermissions()
{
    // all

    var exceptPermissions = new []
    {
        nameof(EnumSystemType.NoMenu),
        nameof(EnumSystemType.App01),
    };

    foreach (var fileName in Enum.GetNames(typeof(EnumSystemType))
        .Except(exceptPermissions)
        .ToArray())
    {
        // all 
    }

    // all 
}

 

to sum up:

1. How to remove a value in an array wording: linq's where

2. How to remove multiple values in an array wording: linq's except

 

 

Guess you like

Origin www.cnblogs.com/straycats/p/12387376.html