What is a LINQ query to get a Cartesian product, even if one set is EMPTY?

Imagine that I have 2 lists and one is empty:

List<string> foo = new List<string>(){ "Ali","wall-e","Ellie" }; List<string> bar = new List<string>(); 

And I get Cartesian product 2:

 var q = from f in foo from b in bar select new {f,b}; 

Since the row is empty, LINQ returns an empty result set.

Question : How can I write the above query to get this result set:

 Ali,NULL Wall-e,NULL Ellie,NULL 
+4
source share
1 answer

Perhaps this is what you want:

 var q = from f in foo.DefaultIfEmpty() from b in bar.DefaultIfEmpty() select new {f,b}; 
+10
source

All Articles