Is it possible to use Intersect on complex arrays in C#?

I have two arrays of student names and test scores.

Each array contains only distinct students (no duplicates) and is structured such that arrStudentGroup1[0][0] = "Bob" and arrStudentGroup1[0][1] = "98".

Given two arrays is it possible to use Intersect to create a third array of students that exists in both arrStudentGroup1 and arrStudentGroup2?

I'd like the third array to have their names and test scores. How do I do this?

Answers


If you want just the names students that are in both groups and not the associated test scores, just intersect on the name array element:

var students1 = arrStudentGroup1.Select(group => group[0]);
var students2 = arrStudentGroup2.Select(group => group[0]);

var studentsInBoth = students1.Intersect(students2);

If you also want the associated test scores you'll need to implement an IEqualityComparer<T> that compares the first element of each array.

If you want the associated test scores then join the two arrays:

var intersection = from s1 in arrStudentGroup1
      join s2 in arrStudentGroup2 on s1[0] equals s2[0]
      select new {Name = s1[0], Score1 = s1[1], Score2 = s2[1]}

foreach (var item in intersection)
{
    Console.Writeline("{0}: s1={1}, s2={2}", Name, Score1, Score2);
}

Need Your Help

How do I generate ASCII codes 2 and 3 in a Bash command line?

linux bash unix ascii

If I press Ctrl+B that ought to give me ASCII code 2, but Ctrl+C is going to be interpreted as a Break.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.