Linq join iquery, how to use defaultifempty

You basically want to do a left outer join. The way you currently are using the DefaultIfEmpty method is that if the entire list is empty you provide a single default entry.

You should join with PassengerDetails and for each passenger details list call the default if empty. This is the equivalent of a left outer join and it goes a little something like this:

var data = from fd in FlightDetails
           join pd in PassengersDetails on fd.Flightno equals pd.FlightNo into joinedT
           from pd in joinedT.DefaultIfEmpty()
           select new {
                         nr = fd.Flightno,
                         name = fd.FlightName,
                         passengerId = pd == null ? String.Empty : pd.PassengerId,
                         passengerType = pd == null ? String.Empty : pd.PassengerType
                       }

Leave a Comment