Why List<Component> l = second.ToList();
lead to
System.NullReferenceException' occurred in ConsoleApplication1.exe
in select new { id = b.id, Name = b.Name };
How to fix this?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Component> firstlist = new List<Component>();
List<Component> secondlist = new List<Component>();
List<Component> thirdlist = new List<Component>();
firstlist.Add(new Component { id = 1, Name = "Jhon" });
secondlist.Add(new Component { id = 2, Name = "Jhon" });
thirdlist.Add(new Component { id = 3, Name = "Jhon" });
var first = from d in firstlist
join i in secondlist
on d.id equals i.id
into a
from b in a.DefaultIfEmpty()
select new { id = b.id, Name = b.Name };
var second = from f in first
join s in thirdlist
on f.id equals s.id
into a
from b in a.DefaultIfEmpty()
select new Component { id = b.id, Name = b.Name };
List<Component> l = second.ToList();
}
public class Component
{
public int id { get; set; }
public string Name { get; set; }
}
}
}