Avoid the queries materialization. Remove the .ToList() calls.
Update:
class Program
{
static void Main(string[] args)
{
var Student_A = new[]{
new {StudentId = 1, Index = 23, Name = "Lucas"},
new {StudentId = 2, Index = 71, Name = "Juan"},
new {StudentId = 3, Index = 85, Name = "Noelia"}
};
var Student_B = new[]{
new {StudentId = 6, Index = 31, Name = "Marcelo"},
new {StudentId = 7, Index = 72, Name = "Manuel"},
new {StudentId = 8, Index = 95, Name = "Roberto"}
};
var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);
var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);
var all = StudentA.Union(StudentB);
all.ToList().ForEach(x=> Console.WriteLine(x.Name));
}
}
Which is the diference with this snippet?
Update 2:
Ok, I got it. The problem is Student_A´s elements and Student_B's elements are different types. I could reproduce the error you have as follow
class Program
{
static void Main(string[] args)
{
var Student_A = new[]{
new {StudentId = 1, Index = 23, Name = "Lucas"},
new {StudentId = 2, Index = 71, Name = "Juan"},
new {StudentId = 3, Index = 85, Name = "Noelia"}
};
var Student_B = new[]{
new {StudentId = 6, Index = 31, Name = "Marcelo", I=0}, // extra property
new {StudentId = 7, Index = 72, Name = "Manuel", I=0},
new {StudentId = 8, Index = 95, Name = "Roberto", I=0}
};
var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index);
var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)
var all = StudentA.Union(StudentB);
all.ToList().ForEach(x=> Console.WriteLine(x.Name));
}
However, if we make both with the same type it works. Take a look at the follow example:
class Program
{
static void Main(string[] args)
{
var Student_A = new[]{
new {StudentId = 1, Index = 23, Name = "Lucas"},
new {StudentId = 2, Index = 71, Name = "Juan"},
new {StudentId = 3, Index = 85, Name = "Noelia"}
};
var Student_B = new[]{
new {StudentId = 6, Index = 31, Name = "Marcelo", I=0},
new {StudentId = 7, Index = 72, Name = "Manuel", I=0},
new {StudentId = 8, Index = 95, Name = "Roberto", I=0}
};
var StudentA = (from p in Student_A.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)
.Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });
var StudentB = (from p in Student_B.Where(a => a.StudentId != 0)
group p by p.Index into g
select g.FirstOrDefault()).Distinct().OrderBy(a => a.Index)
.Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });
var all = StudentA.Union(StudentB);
all.ToList().ForEach(x=> Console.WriteLine(x.Name));
}
The difference is in the line:
.Select(x=> new {StudentId = x.StudentId, Index = x.Index, Name = x.Name });