Working with Anonymous Types with Linq in VB, C #

Let's say I create two sets of tuples, for example:

    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

      

Now I want to perform operations on these two lists:

    Dim loTupleSetDifference = loTupleKeys.Except(loTupleExistingKeys)

      

Obviously Linq cannot execute a comparator on sets unless it knows the sets have the same definition, so it will give me this build error:

The Strict On option disables implicit conversions from "System.Collections.Generic.IEnumerable (From <anonymous type>) 'to" System.Collections.Generic.IEnumerable (From <anonymous type>).

How can I work with declaring these sets to make them a grid? (Not very lucky on Google)

[Edit] Still getting the same compilation error:

    '*** If we have initialized the list of tools, check to make sure it 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)

      

The error is here:

Dim loTupleSetDifference = loTupleDatabaseTools.Except ( loTupleToolObjects )

Error 5 Option Strict On disallows implicit conversions from "System.Collections.Generic.IEnumerable (From <anonymous type>) 'to" System.Collections.Generic.IEnumerable (From <anonymous type>).

+1


a source to share


1 answer


If anonymous types have the same property names with the same types in the same order, they must be of the same type (and therefore compatible).

EDIT: Based on the comments and updated question, I suspect the bit you are missing is the ability to specify properties on anonymous types. Change this:

Dim loTupleExistingKeys = from t in m_losSPResults Select t.key3, t.key4

      



in it:

Dim loTupleExistingKeys = from t in m_losSPResults Select key1=t.key3, key2=t.key4

      

As long as the types are correct, you can be fine without working.

+5


a source







All Articles