linq加入条件

时间:2016-06-02 22:23:40

标签: vb.net linq join

我正在构建一个linq查询:

From G In Games _
            Where G.Field(Of Integer)("SystemID") = System _
            Join F In Files On _
            F.Field(Of String("Name") Equals G.Field(Of String)("PrimaryFilename") _
            Select New With {
                .gid = G.Field(Of Integer)("ID"),
                .ggamename = G.Field(Of String)("GameName")}

我希望联接发生在等于一个值或另一个值的地方。这样的事情(自然不起作用):

From G In Games _
            Where G.Field(Of Integer)("SystemID") = System _
            Join F In Files On _
            F.Field(Of String("Name") Equals G.Field(Of String)("PrimaryFilename") OR G.Field(Of String)("SafeFilename") _
            Select New With {
                .gid = G.Field(Of Integer)("ID"),
                .ggamename = G.Field(Of String)("GameName")}

我该怎么做?

更新

下面有用的建议,但交叉加入结果太昂贵+慢。我也尝试过以下方法:

Dim query1 = _
            From G In Games _
            Where G.Field(Of Integer)("SystemID") = System _
            Join F In Files On _
            G.Field(Of String)("PrimaryFilename") Equals F.Field(Of String)("Name") _
            Select New With {
                .gid = G.Field(Of Integer)("ID"),
                .ggamename = G.Field(Of String)("GameName"),
                .gprimaryfilename = G.Field(Of String)("PrimaryFilename"),
                .gsafegamename = G.Field(Of String)("SafeGameName")}

dim  query2 = _
           From G In Games _
           Where G.Field(Of Integer)("SystemID") = System _
           Join F In Files On _
           G.Field(Of String)("SafeGameName") Equals F.Field(Of String)("Name") _
            Select New With {
            .gid = G.Field(Of Integer)("ID"),
            .ggamename = G.Field(Of String)("GameName"),
            .gprimaryfilename = G.Field(Of String)("PrimaryFilename"),
            .gsafegamename = G.Field(Of String)("SafeGameName")}

Dim query = query1.Union(query2)

但是这产生了一些奇怪的结果(错误的匹配) - 感觉有点超出我的深度。关于如何实现简单"或"的任何其他建议匹配吗?

1 个答案:

答案 0 :(得分:1)

Linq仅支持equi-joins。你可以试着做一个"交叉加入"并将条件放在where子句中:

From G In Games _
    Where G.Field(Of Integer)("SystemID") = System _
    From F In Files _
    Where (F.Field(Of String("Name") = G.Field(Of String)("PrimaryFilename")) _
       Or (F.Field(Of String("Name") = G.Field(Of String)("SafeFilename")) _
    Select New With {
        .gid = G.Field(Of Integer)("ID"),
        .ggamename = G.Field(Of String)("GameName")}