2

I want to use Linq to compare the values of two Lists based on a different field in each list where the list item values are a string[] array.

The format of the arrays are as follows :

Array 1

  • List
  • 4 fields per string[] entry

Array 2

  • List
  • 15 fields per string[] entry

I am trying to compare the value of field 0 from array 1, to check if that value exists in any record in field 12 in array 2, and return the array 1 records where there was no match found in array 2.

What I have so far, is :

var r = array1.Where( p => array2.All( p2 => p2[12] != p[0] ) ).ToList(); 

This is giving me the following error :

An unhandled exception of type 'System.IndexOutOfRangeException'

How can I make this work ? I know I could do a nested loop O(n)^n, however this is not ideal, which is why I wish to use Linq to compare the sub-values.

6
  • You might use Any instead of All Commented Nov 9, 2016 at 10:42
  • Some of the arrays are not valid to your rules. (too less elements in the list) Thats the bigest problem, not your query. Commented Nov 9, 2016 at 10:43
  • What is type of array1 and array2. dont explain just code please! Commented Nov 9, 2016 at 10:43
  • nested loop will be O(n^m). All and Where are O(n) too so they will perform same Commented Nov 9, 2016 at 10:46
  • @M.kazemAkhgary thank you for correcting the math. I know it was an exponent of, just not the precise formula. Regarding the issue, I did figure it out -- i was passing the 4 array to array 2, and the 12 array to array 1. So the above code, actually does work XD /facepalm. Commented Nov 9, 2016 at 10:51

2 Answers 2

3

I made an example with your code description, and it works just fine. Can you please check if your array has the correct size?

private static void Main(string[] args) { var array1 = new List<string[]> { new[] {"1", "2", "3", "4"}, new[] {"A", "B", "C", "D"} }; var array2 = new List<string[]> { new[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"}, new[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"} }; var r = array1.Where(p => array2.All(p2 => p2[12] != p[0])).ToList(); r.ForEach(_ => Array.ForEach(_, Console.WriteLine)); // output: // 1 // 2 // 3 // 4 // A // B // C // D } 
Sign up to request clarification or add additional context in comments.

3 Comments

I noted that above as well. The code works, just I was passing the wrong array to my method that contained this code. XD. My function was in the format of : short array, long array . but I was passing : long array, short array .
@SamuelJackson but please double-check the logic of your code. I didn't understand what you really want to compare, and what do you expect from it. If you could provide some business logic and more CODE (not just descriptions), maybe the community can provide a better feedback (for instance, about LINQ optimization)
I appreciate that. I can not provide samples of the data as it contains private information. What I am comparing is to see if the list of account id's from one report, exist in the payment file being sent to the bank. The payment file is in fixed length format, and the first report is in csv. I could convert all the data, and strip information out to make the compare easier, but I still need to return the data, and still need to compare recursively. This is where LINQ really shines.
0

p is not an array now. Use:

var r = array1.Where( p => array2.All( p2 => p2[12] != p ) ).ToList(); 

2 Comments

where is the compare for array1 field 0 as array 1 contains 4 fields
Sorry yes i missed that. But the problem certainly is in the either p2[12] or p[0]. My guess is not all the elements in the arrays have same number of values. I suggest you check elements in arrays. Also you can make elements a class to make sure they share the same pattern

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.