C# Linq加入iquery,如何使用defaultifempty
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19293844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq join iquery, how to use defaultifempty
提问by user1032957
I have written a linq join query and I would like to take the values, if one of them are empty...
我写了一个 linq join 查询,如果其中一个为空,我想获取这些值...
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
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?
FlightDetails
是Idatarepository
类的类型,PassengerDetails
是IQueryable
局部变量结果。如何在PassengerId
没有flightno
和flightname
包含在整体结果中的和 Classtype获得结果?
采纳答案by Kristof
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.
你基本上想做一个左外连接。您当前使用 DefaultIfEmpty 方法的方式是,如果整个列表为空,则您提供一个默认条目。
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:
PassengerDetails
如果为空,您应该加入并为每个乘客详细信息列表调用默认值。这相当于左外连接,它有点像这样:
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
}