本文介绍了LINQ加入在C#中有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ在C#中有多个条件加入声明。

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?

推荐答案

据我所知,你只能加入这种方式:

AFAIK 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
    }

的主要要求是:
属性名称,类型和顺序的匿名对象傻冒加入上必须匹配

您不能使用AND将手术室等的连接。 。只是object1等于Object2的

You CAN'T use ANDs ORs etc. in joins. Just object1 equals object2.

在此LinqPad例如更先进的东西:

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};
}



寻址名和属性顺序是简单的,涉及类型可以通过铸造实现/转换/解析/调用方法等,这可能并不总是与LINQ合作,EF或SQL或NHibernate的,大部分方法调用肯定不会工作,并会在运行时出现故障,因此因人而异。
这是因为它们复制到公共只读属性的匿名对象,所以只要你的表达产生正确类型值的属性一起 - 你应该罚款

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.

这篇关于LINQ加入在C#中有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:58
查看更多