여러 조건이있는 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 };
결합이 올바르게 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
AFAIK는 다음 방법으로 만 참여할 수 있습니다.
var query = from obj_i in set1
join obj_j in set2 on
new {
JoinProperty1 = obj_i.SomeField1,
JoinProperty2 = obj_i.SomeField2,
JoinProperty3 = obj_i.SomeField3,
JoinProperty4 = obj_i.SomeField4
}
equals
new {
JoinProperty1 = obj_j.SomeOtherField1,
JoinProperty2 = obj_j.SomeOtherField2,
JoinProperty3 = obj_j.SomeOtherField3,
JoinProperty4 = obj_j.SomeOtherField4
}
주요 요구 사항은 다음과 같습니다. 가입하려는 익명 개체의 속성 이름, 유형 및 순서가 일치해야합니다.
조인에 ANDs OR 등을 사용할 수 없습니다. object1 만 object2와 같습니다.
이 LinqPad 예제의 고급 기능 :
class c1
{
public int someIntField;
public string someStringField;
}
class c2
{
public Int64 someInt64Property {get;set;}
private object someField;
public string someStringFunction(){return someField.ToString();}
}
void Main()
{
var set1 = new List<c1>();
var set2 = new List<c2>();
var query = from obj_i in set1
join obj_j in set2 on
new {
JoinProperty1 = (Int64) obj_i.someIntField,
JoinProperty2 = obj_i.someStringField
}
equals
new {
JoinProperty1 = obj_j.someInt64Property,
JoinProperty2 = obj_j.someStringFunction()
}
select new {obj1 = obj_i, obj2 = obj_j};
}
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. 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.
Your and
should be a &&
in the where
clause.
where epl.DepartAirportAfter > sd.UTCDepartureTime
and epl.ArriveAirportBy > sd.UTCArrivalTime
should be
where epl.DepartAirportAfter > sd.UTCDepartureTime
&& epl.ArriveAirportBy > sd.UTCArrivalTime
If you need not equal object condition use cross join sequences:
var query = from obj1 in set1
from obj2 in set2
where obj1.key1 == obj2.key2 && obj1.key3.contains(obj2.key5) [...conditions...]
ReferenceURL : https://stackoverflow.com/questions/3020442/linq-joining-in-c-sharp-with-multiple-conditions
'programing' 카테고리의 다른 글
Perl이 설치되지 않은 시스템에서 실행될 수 있도록 Perl 스크립트를 컴파일하려면 어떻게해야합니까? (0) | 2021.01.14 |
---|---|
크로스 플랫폼 Qt 애플리케이션에 모노 스페이스 글꼴을 지정하는 방법은 무엇입니까? (0) | 2021.01.14 |
Python의 목록 이해와 .NET LINQ (0) | 2021.01.14 |
예외 처리 : throw, throws 및 Throwable (0) | 2021.01.14 |
숫자 리터럴의 Java 7 밑줄 (0) | 2021.01.14 |