• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: LINQ Except not returning string values correctly

Resolved: LINQ Except not returning string values correctly

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’ve been trying to create a small program to compare two collections of strings and to output any items that are different or missing between collection1 and collection2.
As far as I have been able to determine LINQ’s .Except method should provide my desired outcome but it seems to fall short.
The files which I’m trying to compare are the ACL files produced by using ICACLS to save all the permissions on a given directory and its subdirectories by running the following command from cmd:
I run this command twice on a directory to produce two icacls files which I want to ensure match and if they don’t then I want to output where they don’t match.
Here is some sample code for trying to compare these items
Once the items were loaded I’d run the .Except command
However, here is an example below of 10 records from file 1 in which I remove one permissions record from file 2 but .Except doesn’t identify that it’s missing the item
First file:
Second file:
As described above the second file is missing one permissions record but .Except doesn’t recognise this, the collections are being processed as an enumeration of strings and the default equality comparer should be able to detect this difference as far as I can understand, I’m aware you can override this with a custom comparer but I’m not sure on what the implementation would be for this.
An additional note is that this only seems to throw up issues with any of the permissions strings themselves, .Except does seem able to determine any missing strings when it’s one of the folder/file names, so I’m thinking it may be getting confused because there are many identical permissions strings within the collection so it may think it’s got a matching item even though it’s not got a specific record relating to a specific file.
I expect this will need some sort of custom override but I’m not sure what this implementation would be.
Any thoughts would be much appreciated, thanks for taking the time to read this through.

Answer:

I’m not sure if my assumptions are correct, but it looks like the format of the file generated is two lines per directory entry and that the first line is the full path to the file and hence must be unique.
If that is correct, then the second list can not contain the line TestHash\testFile3.csv.
If so then you can group the directory entry with the permission and then use except to check for differences.
To do this, I first add a line number to each line and then group by every two lines and the create an anonymous object with the first and second entries in each group,
eg
Because the DirectoryEntry Name is unique, we know that each entry in our grouped list must be unique and hence the except operator will operator as you want.
Or you could combine groupedList1 and groupedList2 like

If you have better answer, please add a comment about this, thank you!

c# compare except icacls linq
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Openlayers: Update clusters depending on filter (geometryFunction)

24/03/2023

Resolved: Getting ‘502 Bad Gateway’ while deploying Springboot app in EKS

24/03/2023

Resolved: Why is NGINX’s $request_uri empty?

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.