在VB中使用Linq的匿名类型,C#

Dan*_*iel 1 c# linq vb.net types set

假设我创建了两组元组,如下所示:

    Dim losSPResults As List(Of spGetDataResults) = m_dcDataClasses.spGetData.ToList
    Dim loTupleKeys = From t In losSPResults Select t.key1, t.key2

    '' Query on an existing dataset:
    Dim loTupleExistingKeys = from t in m_losSPResults Select t.key3, t.key4
Run Code Online (Sandbox Code Playgroud)

现在我想对这两个列表执行set操作,如下所示:

    Dim loTupleSetDifference = loTupleKeys.Except(loTupleExistingKeys)
Run Code Online (Sandbox Code Playgroud)

显然,如果不知道集合具有统一的定义,Linq就无法对集合执行比较,因此它会给我这个构建错误:

Option Strict On禁止从'System.Collections.Generic.IEnumerable(Of <anonymous type>)'到'System.Collections.Generic.IEnumerable(Of <anonymous type>)'的隐式转换.

如何使用这些集合的声明来使它们成为网格?(谷歌运气不太好)

[编辑]仍然得到相同的编译错误:

    '*** If we have initialized the list of tools, check to make sure it's up to date
    Dim loTupleDatabaseTools = From tt In lottTorqueTools _
                               Select StationIndex = tt.station_index, SlotNumber = tt.slot_number
    Dim loTupleToolObjects = From tt In m_lottTorqueTools _
                             Select StationIndex = tt.StationIndex, SlotNumber = tt.SlotNumber

    Dim loTupleSetDifference = loTupleDatabaseTools.Except(loTupleToolObjects)
Run Code Online (Sandbox Code Playgroud)

错误在这里:

Dim loTupleSetDifference = loTupleDatabaseTools.Except(loTupleToolObjects)

错误5选项Strict On禁止从'System.Collections.Generic.IEnumerable(Of <anonymous type>)'到'System.Collections.Generic.IEnumerable(Of <anonymous type>)'的隐式转换.

Jon*_*eet 5

如果匿名类型具有相同类型的相同属性名称,则它们应该是相同的类型(因此兼容).

编辑:根据评论和更新的问题,我怀疑你缺少的是能够以匿名类型命名属性.改变这个:

Dim loTupleExistingKeys = from t in m_losSPResults Select t.key3, t.key4
Run Code Online (Sandbox Code Playgroud)

进入这个:

Dim loTupleExistingKeys = from t in m_losSPResults Select key1=t.key3, key2=t.key4
Run Code Online (Sandbox Code Playgroud)

只要类型合适,你就可以没有更多的工作了.

  • 那些是匿名类型,对吗?您可以选择名称:"选择Key3 = t.Key1"等 (2认同)