Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the LINQ query to get a Cartesian Product even when one set is EMPTY?

Imagine I have 2 list and one is empty:

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

And I obtain the Cartesian Product of 2:

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

As bar is empty LINQ returns an empty result set.

Question: How can I write the above query so that I can receive this result set:

Ali,NULL

Wall-e,NULL

Ellie,NULL
like image 937
pencilCake Avatar asked Dec 20 '22 08:12

pencilCake


1 Answers

Maybe this is what you want:

var q = from f in foo.DefaultIfEmpty()
    from b in bar.DefaultIfEmpty()
    select new {f,b};
like image 119
Jeppe Stig Nielsen Avatar answered Jan 14 '23 13:01

Jeppe Stig Nielsen