Home » AzureRSS

How to use Select LINQ query into generic list that contains a property that is also a generic list?

I have a simple LINQ to Objects Select statement where I return some results into a List(of MyClass).  Pretty straight forward.  The problem I am running into is that 'MyClass' has a property within it that is also a genric list which I am trying to select into as well.  I have selected into a single object many times before but not into another collection within the main query.  The code below does not work below telling me (which makes sense) "Value of type 'MyClass' cannot be converted to 'System.Collections.Generic.List(Of MyOtherClass)"  I can't find a spot to add '.ToList' on the end either to the inner definition of the List (of MyOtherClass) property names .MyGenericListProp.  Take a look at the code below:

Dim Results = From mr In ds.Tables(0).AsEnumerable() _

    SelectNew MyClass1 With { _

    .ID = If(Not IsDBNull(mr.Field(Of Integer)("ID")), mr.Field(Of Integer)("ID"), 0),

    .MyGenericListProp = New MyOtherClass With { _

        .ID = If(Not IsDBNull(mc.Field(Of Integer)("SecondaryID")), mc.Field(Of Integer)("SecondaryID"), 0),

        .Value1 = If(Not IsDBNull(mc.Field(Of List(Of String))("Value1")), mc.Field(Of List(Of String))("Value1"), String.Empty),

        .Value2 = If(Not IsDBNull(mc.Field(Of List(Of String))("Value2")), mc.Field(Of List(Of String))("Value2"), String.Empty)}}

In the above if 'MyGenricListProp' was just a single instance property, everything would work great.  The problem is I can't define it as 'New List(of MyOtherClass)' because then I can't see the properties.  And as I mentioned there is no .ToList I can access just for defining .MyGenericListProp.

Anyways, don't get too caught up in the overall query.  I cut parts out that aren't needed so I don't want to get on a tangent off on something else.  I just need the syntax to complete the query above if possible.

Thanks!

 

2 Answers Found

 

Answer 1

Hi,

Please try the following.

Dim Results = ds.Tables(0).AsEnumerable().Select( Function(mr) 
Dim temp as new MyClass1()
temp.ID = If(Not IsDBNull(mr.Field(Of Integer)("ID")), mr.Field(Of Integer)("ID"), 0)
    temp.MyGenericListProp.Add( New MyOtherClass With { _
    .ID = If(Not IsDBNull(mc.Field(Of Integer)("SecondaryID")), mc.Field(Of Integer)("SecondaryID"), 0),

    .Value1 = If(Not IsDBNull(mc.Field(Of List(Of String))("Value1")), mc.Field(Of List(Of String))("Value1"), String.Empty),

    .Value2 = If(Not IsDBNull(mc.Field(Of List(Of String))("Value2")), mc.Field(Of List(Of String))("Value2"), String.Empty)})
  Return temp
End Function)

Hope it helps.

 

Regards,

Nyi Nyi

 

Answer 2

Nyi Nyi - I wasn't able to try your suggestion as I found the solution to my question almost as fast as I posted it.  If others find  it useful they can vote it as helpful and thanks for the response.

The solution to populating the instance property  using LINQ was to fully qualify the DataTable source again in the query.  So reformatting my original query  to work  properly now looks like this:

Dim Results = From mr In ds.Tables(0).AsEnumerable() _
 SelectNew MyClass1 With { _
 .ID = If(Not IsDBNull(mr.Field(Of Integer)("ID")), mr.Field(Of Integer)("ID"), 0),
 .MyGenericListProp = (From mr2 In dsSecondary.Tables(0).AsEnumerable() SelectNew MyOtherClass With { _
  .ID = If(Not IsDBNull(mr2.Field(Of Integer?)("SecondaryID")), mr2.Field(Of Integer)("SecondaryID"), 0),
  .Value1 = If(Not IsDBNull(mr2.Field(Of List(Of String))("Value1")), mr2.Field(Of List(Of String))("Value1"), String.Empty),
  .Value2 = If(Not IsDBNull(mr2.Field(Of List(Of String))("Value2")), mr2.Field(Of List(Of String))("Value2"), String.Empty)}}

The new part was adding in: From mr2 In dsSecondary.Tables(0).AsEnumerable() Select

Once I added this in everything worked well.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter