C# 中的 LINQ 连接具有多个条件
我在 C# 中有一个具有多个条件的 LINQ Joining 语句。
var possibleSegments =
from epl in eventPotentialLegs
join sd in segmentDurations on
new {
epl.ITARequestID,
epl.ITASliceNumber,
epl.DepartAirportAfter,
epl.AirportId_Origin,
epl.AirportId_Destination
}
equals
new {
sd.ITARequestId,
sd.SliceIndex,
sd.OriginAirport,
sd.DestinationAirport
}
where
epl.DepartAirportAfter > sd.UTCDepartureTime
and
epl.ArriveAirportBy > sd.UTCArrivalTime
select new PossibleSegments{ ArrivalTime = sd.arrivalTime };
连接无法正常工作。我做错了什么?
I have a LINQ Joining statement in C# with multiple conditions.
var possibleSegments =
from epl in eventPotentialLegs
join sd in segmentDurations on
new {
epl.ITARequestID,
epl.ITASliceNumber,
epl.DepartAirportAfter,
epl.AirportId_Origin,
epl.AirportId_Destination
}
equals
new {
sd.ITARequestId,
sd.SliceIndex,
sd.OriginAirport,
sd.DestinationAirport
}
where
epl.DepartAirportAfter > sd.UTCDepartureTime
and
epl.ArriveAirportBy > sd.UTCArrivalTime
select new PossibleSegments{ ArrivalTime = sd.arrivalTime };
The joining does not work correctly. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知只能通过这种方式加入:
主要要求是:
您要联接的匿名对象中的属性名称、类型和顺序必须匹配。
您不能在联接中使用 AND、OR 等。只是 object1 等于 object2。
此 LinqPad 示例中更高级的内容:
寻址名称和属性顺序很简单,寻址类型可以通过转换/转换/解析/调用方法等来实现。这可能并不总是适用于 LINQ to EF 或 SQL 或 NHibernate,大多数方法调用肯定不起作用,并且会在运行时失败,因此 YMMV(您的里程可能会有所不同)。
这是因为它们被复制到匿名对象中的公共只读属性,因此只要您的表达式生成连接属性的正确类型的值 - 应该没问题。
As far as I know you can only join this way:
The main requirements are:
Property names, types and order in the anonymous objects you're joining on must match.
You CAN'T use ANDs, ORs, etc. in joins. Just object1 equals object2.
More advanced stuff in this LinqPad example:
Addressing names and property order is straightforward, addressing types can be achieved via casting/converting/parsing/calling methods etc. This might not always work with LINQ to EF or SQL or NHibernate, most method calls definitely won't work and will fail at run-time, so YMMV (Your Mileage May Vary).
This is because they are copied to public read-only properties in the anonymous objects, so as long as your expression produces values of correct type the join property - you should be fine.
您的
and
应该是where
子句中的&&
。应该是
Your
and
should be a&&
in thewhere
clause.should be
如果不需要相等的对象条件,请使用交叉连接序列:
If you need not equal object condition use cross join sequences: