Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Joining in C# with multiple conditions

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?

like image 608
Ratheesh Avatar asked Jun 11 '10 05:06

Ratheesh


People also ask

What type of join is LINQ join?

In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.

How use inner join in LINQ?

A simple inner join that correlates elements from two data sources based on a simple key. An inner join that correlates elements from two data sources based on a composite key. A composite key, which is a key that consists of more than one value, enables you to correlate elements based on more than one property.

Is LINQ join inner or outer?

When you use the LINQ join clause in the query expression syntax to combine two sets of related information, you perform an inner join. This means that you provide an expression that determines for each item in the first sequence, the matching items in the second.


1 Answers

As far as I know you can only join this way:

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     } 

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:

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 (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.

like image 165
Zar Shardan Avatar answered Sep 18 '22 17:09

Zar Shardan