Can someone explain this lambda expression to me? I like it

The code below is part of the authorization. I try to mentally depict what he actually does, but could not.

IsAuthorized = ((x, y) => x.Any(z => y.Contains(z))); 

Can someone explain this lambda expression to me?

Thanks!

Edit:

IsAuthorized is a delegate type. The previous programmer who encodes this seems to want to keep it a secret by putting a delegate at the end of the cs file.

Actual code:

 public delegate bool IsAuthorized(IEnumerable<Int32> required, IEnumerable<Int32> has); IsAuthorized = ((x, y) => x.Any(z => y.Contains(z))); 
+6
source share
2 answers

Sure - he said that for the pair (x, y) , x contains any values, such that y contains this value.

It seems to me that this really says "is there an intersection between x and y ".

Thus, an alternative could be:

 IsAuthorized = (x, y) => x.Intersect(y).Any(); 

It is just possible that this will not work, depending on the type of IsAuthorized , but I expect it to be correct.

+15
source

To go with John's explanation, here is (hopefully) an equivalent sample with outputs:

  static void Main(string[] args) { int[] i = new int[] { 1, 2, 3, 4, 5 }; int[] j = new int[] { 5, 6, 7, 8, 9 }; int[] k = new int[] { 0, 6, 7, 8, 9 }; bool jContainsI = i.Any(iElement => j.Contains(iElement)); bool kContainsI = i.Any(iElement => k.Contains(iElement)); Console.WriteLine(jContainsI); // true Console.WriteLine(kContainsI); // false Console.Read(); } 

Basically, any element i in j or k . This assumes your parameters x and y are collections of some variety.

Intersection is a valid alternative here:

 bool iIntersectsJ = i.Intersect(j).Any(); // true bool iIntersectsK = i.Intersect(k).Any(); // false 
+3
source

Source: https://habr.com/ru/post/896420/


All Articles