Linq加入iquery,如何使用defaultifempty [英] Linq join iquery, how to use defaultifempty
问题描述
我编写了一个 linq 连接查询,如果其中一个为空,我想获取这些值...
I have written a linq join query and I would like to take the values, if one of them are empty...
代码:
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
是类上的 Idatarepository
类型,PassengerDetails
是 IQueryable
局部变量结果.如何获得 PassengerId
和 Classtype 的结果,而 flightno
和 flightname
不包含在整体结果中?
FlightDetails
is Idatarepository
type on a class and PassengerDetails
is IQueryable
local variable result. How can I get result with PassengerId
and Classtype without 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屋!