Click here to Skip to main content
15,506,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
arr1 = wavFiles.ToArray();
arr2 = BaseDocs.ToArray();
arr3 = TemplateDocs.ToArray();
arr4 = DraftDocs.ToArray();

I have four arrays i need to take the elements in the first array and check whether it is in second,third and fourth array elements if they exists i need to store in string variables how can i do this

What I have tried:

I could not get how to do this
Updated 22-Aug-17 0:17am

1 solution

You can use the LINQ method Intersect[^] to easily find elements in both arrays:
var inBothArr1AndArr2 = arr1.Intersect(arr2);
var inBothArr1AndArr3 = arr1.Intersect(arr3);
var inBothArr1AndArr4 = arr1.Intersect(arr4);
Now you have three IEnumerable<T>s. Do with their contents what you need to do.


From your comment, it appears you want to do this one-by-one. That's possible too:
for (int i = 0; i < arr1.Length; i++)
    if (arr2.Contains(arr1[i]))
        // Array 2 contains this element. Index: i. Value: arr1[i]
    // do the same for arr3 and arr4
Share this answer
Thomas Daniels 22-Aug-17 5:24am    
See my edit.
kav@94 22-Aug-17 5:26am    
for (int i = 0; i < wavarr.Length; i++)
if (basearr.Contains(wavarr[i]))
// Array 2 contains this element. Index: i. Value: arr1[i]
// do the same for arr3 and arr4

i had did this but it is giving below error

'System.Array' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) C:\Users\yaminik\Desktop\MT original from network with 208 db\mttraining\SubjectItems.aspx.cs 153 28 C:\...\mttraining\
Thomas Daniels 22-Aug-17 5:27am    
Add using System.Linq; at the top of your file.
Thomas Daniels 22-Aug-17 5:40am    
If you want to store multiple of these variables, you can create a List<string> and store them there.
Thomas Daniels 22-Aug-17 5:47am    
On top of your file: using System.Collections.Generic;

Before your for loop: List<string> list = new List<string>();

Inside the 'if' statement of the loop: list.Add(arr1[i]);

After the for loop, you can access the list like an array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900