osama_1200
osama_1200

Reputation: 29

How to add items in anonymous type and retrieve list of item

I have a DataTable in the variable dtLatencyValues.

I select data from this DataTable into a list of an anonymous type:

Dim _Latency = dtLatencyValues.AsEnumerable()
                   .Where(Function(x) x("Customer_Name") = str1(0).CustName AndAlso x("Site_Name") = str1(j).sitname)
                   .Select(Function(x) New With 
                   {
                        .CommLat = x("Committed_Value"), 
                        .ActLat = x("ActualLatencyVal"), 
                        .ToContry = x("Site_To_Name")
                   })
                   .OrderBy(Function(a) a.ToContry)
                   .DefaultIfEmpty().ToList()

I want to add :

Dim temp = New With {.CommLat = "-", .ActLat = "-", .ToContry = str1(j).sitname}

into my _Latency list and then retrieve data from the final _Latency list.

Upvotes: 0

Views: 1476

Answers (2)

Mark
Mark

Reputation: 8160

You will probably want to set Option Strict On to help avoid some issues like this. Your problem is that the properties of the anonymous type created by the LINQ query are all typed as Object, since DataRow.Item (the x("field name") in your code) returns Object. When you want to add an item, the anonymous types need to match, and AnonymousType(Of Object, Object, Object) doesn't match AnonymousType(Of String, String, String).

If you change your query to use the correct types, you should be able to add new items more easily, e.g. (not sure if these are the correct types for your data!):

Dim _Latency = dtLatencyValues.AsEnumerable() _
    .Where(Function(x) x.Field(Of String)("Customer_Name") = str1(0).CustName AndAlso x.Field(Of String)("Site_Name") = str1(j).sitname) _
    .Select(Function(x) New With 
        {
            .CommLat = x.Field(Of String)("Committed_Value"), 
            .ActLat = x.Field(Of String)("ActualLatencyVal"), 
            .ToContry = x.Field(Of String)("Site_To_Name")
        }) _
    .OrderBy(Function(a) a.ToContry) _
    .DefaultIfEmpty().ToList()
_Latency.Add(New With {.CommLat = "-", .ActLat = "-", .ToContry = str1(j).sitname})

You may need to add a reference to System.Data.DataSetExtensions.dll for the .Field(Of String) calls to work.

EDIT

Here's a complete working example - VS2013 console project.

Option Strict On
Option Explicit On
Option Infer On

Module Module1

    Sub Main()

        Dim dtLatencyValues = New DataTable()
        dtLatencyValues.Columns.Add("Customer_Name", GetType(String))
        dtLatencyValues.Columns.Add("Site_Name", GetType(String))
        dtLatencyValues.Columns.Add("Committed_Value", GetType(String))
        dtLatencyValues.Columns.Add("ActualLatencyVal", GetType(String))
        dtLatencyValues.Columns.Add("Site_To_Name", GetType(String))

        dtLatencyValues.Rows.Add("CustA", "SiteA", "Com1", "Act1", "STN1")
        dtLatencyValues.Rows.Add("CustA", "SiteA", "Com2", "Act2", "STN2")
        dtLatencyValues.Rows.Add("CustA", "SiteB", "Com1", "Act1", "STN1")
        dtLatencyValues.Rows.Add("CustB", "SiteB", "Com1", "Act1", "STN1")

        Dim custName = "CustA"
        Dim siteName = "SiteA"

        Dim _Latency = dtLatencyValues.AsEnumerable() _
            .Where(Function(x) x.Field(Of String)("Customer_Name") = custName AndAlso x.Field(Of String)("Site_Name") = siteName) _
            .Select(Function(x) New With
            {
                .CommLat = x.Field(Of String)("Committed_Value"),
                .ActLat = x.Field(Of String)("ActualLatencyVal"),
                .ToContry = x.Field(Of String)("Site_To_Name")
            }) _
            .OrderBy(Function(a) a.ToContry) _
            .DefaultIfEmpty().ToList()
        _Latency.Add(New With {.CommLat = "-", .ActLat = "-", .ToContry = siteName})

    End Sub

End Module

Setting a breakpoint at the End Sub line and inspecting _Latency shows:

Watch window

Upvotes: 0

sloth
sloth

Reputation: 101052

You can simply use Add or Insert:

Dim _Latency = dtLatencyValues.AsEnumerable()...ToList()
Dim temp = New With {.CommLat = "-", .ActLat = "-", .ToContry = str1(j).sitname}

_Latency.Insert(0, temp) ' insert as first element '
' or '
_Latency.Add(temp) ' insert as last element '

Upvotes: 1

Related Questions