If you want to understand precisely why your code isn't working then Tommy's answer is perfect - remove the ".Intersect(name2)" line (because "name2" doesn't have any items and so intersecting with it will always return in zero results) and then implement GetHashCode and Equals methods on your "objA" class.
However, if you just want to make your code work in the simplest way, you can change objA from a class to a struct since structs automatically receive GetHashCode and Equals implementations that do what you want.
I've fixed up your original code that so that it compiles and changed "objA" into a struct and now the final "x" value will be a list with a single value (foo1 = 1, foo2 = "bb", foo3 = true).
using System.Collections.Generic;
using System.Linq;
namespace StackOverflowAnswer
{
public struct objA
{
public int foo1 { get; set; }
public string foo2 { get; set; }
public bool foo3 { get; set; }
}
class Program
{
static void Main()
{
List<objA> name1 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true }, new objA { foo1 = 2, foo2 = "cc", foo3 = false } };
List<objA> name2 = new List<objA>();
List<objA> name3 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true } };
List<objA> name4 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true }, new objA { foo1 = 2, foo2 = "cc", foo3 = false } };
List<objA> name5 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true } };
List<objA> x = name1
//.Intersect(name2)
.Intersect(name3)
.Intersect(name4)
.Intersect(name5)
.ToList();
}
}
}
Having read the comments on other answers for this question, maybe I've missed part of the point - is the desire that empty sets should effectively be ignored when performing an "intersect"?
If so, then you could do this by writing your own version of "Intersect", which ignores empty sets - eg.
using System.Collections.Generic;
using System.Linq;
namespace StackOverflowAnswer
{
public struct objA
{
public int foo1 { get; set; }
public string foo2 { get; set; }
public bool foo3 { get; set; }
}
class Program
{
static void Main()
{
List<objA> name1 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true }, new objA { foo1 = 2, foo2 = "cc", foo3 = false } };
List<objA> name2 = new List<objA>();
List<objA> name3 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true } };
List<objA> name4 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true }, new objA { foo1 = 2, foo2 = "cc", foo3 = false } };
List<objA> name5 = new List<objA>() { new objA { foo1 = 1, foo2 = "bb", foo3 = true } };
List<objA> x = name1
.IntersectUnlessEmpty(name2)
.IntersectUnlessEmpty(name3)
.IntersectUnlessEmpty(name4)
.IntersectUnlessEmpty(name5)
.ToList();
}
}
public static class IEnumerableExtensions
{
public static IEnumerable<T> IntersectUnlessEmpty<T>(this IEnumerable<T> source, IEnumerable<T> second)
{
return second.Any() ? source.Intersect(second) : source;
}
}
}
The final "x" value in this case is still a list with a single item - the empty "name2" data did not prevent the non-empty lists from being intersected.