Question

Hey all I am new at using linq and found a good example here. However, its all in C# and when trying to convert it I get a lot of errors that I can not seem to fix.

Imports System.Linq
Imports HelperApp.SPTrackerData
Imports System.Net

Public Class frm_tasks
    Private Sub cmdCheck_Click(sender As Object, e As EventArgs) Handles cmdCheckMail.Click
        Dim dc As New TeamSiteDataContext(New Uri("http://intranet/_vti_bin/listdata.svc"))

        dc.Credentials = CredentialCache.DefaultNetworkCredentials

        Dim result = From d In dc.Inventory With { _
                Key.Title = d.Title, _
                Key.Description = d.Description, _
                Key.Cost = d.Cost _
            }
        For Each d As var In result
            Console.WriteLine(d)
        Next
    End Sub
End Class

I connected my SharePoint to a Service Reference called SPTrackerData as it stated on the website but I can not seem to fix the following:

TeamSiteDataContext: I'm not sure where this is in the original code so I really have no idea on where or what to replace it with in my code above.

The Linq part: (Dim result =...) I tried my best to form it as i thought it needed to look like but its all wrong.

The one thing that doesn't make since to me is why did I create a service reference and not even use it at all within that code???

Was it helpful?

Solution

TeamSiteDataContext represents the runtime context of the data service. When you add a reference to an OData-based service (listdata.svc) using Add Service Reference dialog in Visual Studio, a representation of TeamSiteDataContext entity container class that inherits from DataServiceContext is being generated.

Below is provided the converted to VB.Net example:

Sub Main()

    Dim dc As New TeamSiteDataContext(New Uri("http://intranet/_vti_bin/listdata.svc"))
    dc.Credentials = CredentialCache.DefaultNetworkCredentials

    Dim result = From d In dc.Inventory Select New With { _
                       Key .Title = d.Title, _
                       Key .Description = d.Description, _
                       Key .Cost = d.Cost _
                     }
    For Each d In result
        Console.WriteLine(d)
    Next

End Sub

References

Generating the Data Service Client Library (WCF Data Services)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top