LINQ的加入IQUERY,如何使用defaultifempty [英] Linq join iquery, how to use defaultifempty

查看:129
本文介绍了LINQ的加入IQUERY,如何使用defaultifempty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经wriiten一个LINQ连接查询,我想取的值,如果其中一人是空的......

I have wriiten a linq join query and I would like to take the values, if one of them are empty...

code:

var Details = 

UnitOfWork.FlightDetails
          .Query()
          .Join
          (
              PassengersDetails,
              x => x.Flightno,
              y => y.FlightNo,
              (x, y) => new
              {
                  y.PassengerId,
                  y.classType,
                  x.Flightno,
                  x.FlightName,
              }
          );

我想用类似的东西..

I would like to use something like..

"Above query".DefaultIfEmpty
(
    new 
    {
        y.PassengerId,
        y.classType,
        string.Empty,
        string.Empty
    }
);

FlightDetails是一类与PassengerDetailsIdatarepository类型IQueryable的局部变量的结果。我怎样才能获得与乘客ID和CLASSTYPE没有flightno和flightname导致包括在总的结果。

"FlightDetails" is Idatarepository type on a class and "PassengerDetails" is iqueryable local variable result. How can I get result with passenger Id and Classtype with no flightno and flightname included in the overall results.

推荐答案

您基本上是想做一个左外连接。您目前正在使用的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.

您应该 PassengerDetails 加入,如果空对每名旅客的详细信息列表调用默认的。这是一个左外相当于加入,它会少了一些这样的:

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
                       }

这篇关于LINQ的加入IQUERY,如何使用defaultifempty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆