Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. LINQ

LINQ

Scheduled Pinned Locked Moved WPF
databasehelpcsharplinqtutorial
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sunil n cs

    Ya you are right am writing this code in web.service, "strlogin" contains the keyword depending on which i have to retrieve data from database. What am trying to do here is retrieve username and password from table tbl_user. Am getting the following error Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`2[System.String,System.String]]' to type 'System.Collections.Generic.List`1[E_Learning.Web.tbl_User]'. Is there any other way to retrieve data from database without using webservice, am very new 2 silverlight and dont know much about it, any support will be of great help.

    M Offline
    M Offline
    Michael Sync
    wrote on last edited by
    #4

    I don't have VB in my VS. But I guess you will need to cast it before returning the object.

    Return matchingLogin.ToList()

    sunil.n.cs wrote:

    Is there any other way to retrieve data from database without using webservic

    If you are using silverlight-ported object database like db4o or silverlightdb then you can retrieve the data without using any web service. Otherwise, No.

    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders

    S 1 Reply Last reply
    0
    • M Michael Sync

      I don't have VB in my VS. But I guess you will need to cast it before returning the object.

      Return matchingLogin.ToList()

      sunil.n.cs wrote:

      Is there any other way to retrieve data from database without using webservic

      If you are using silverlight-ported object database like db4o or silverlightdb then you can retrieve the data without using any web service. Otherwise, No.

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders

      S Offline
      S Offline
      sunil n cs
      wrote on last edited by
      #5

      I already tried casting before returning the object, it works fine until i dont mention any column names in select, if i do mention any column names as in this case den i get the following error when i try to cast. Value of type 'System.Collections.Generic.List(Of )' cannot be converted to 'System.Collections.Generic.List(Of E_Learning.Web.tbl_User)'.

      1 Reply Last reply
      0
      • S sunil n cs

        Hi, Am very new to silverlight, and am working on this module where i want to retrieve some data from database, the problem is when i mention column names it dosnt work properly and it gives an error at the return statement....the code is below... Public Function GetLoginName(ByVal strLogin As String) As List(Of tbl_User) Implements IService1.GetLoginName Dim db As New DataClasses1DataContext Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _ Select Login.UserName, Login.Password Return matchingLogin End Function Can any1 tell me how to achieve it .. Thanks in advance

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #6

        As Michael said, you need to do:Return matchingLogin.ToList(). The reason you have to do this is because Linq uses a technique called deferred execution (please Google and have a read about this feature), which means that matchingLogin is actually IQueryable(Of tbl_User). If you think about this, it makes a lot of sense because there are so many different operations that you could perform here on your select that you need to tell it which one you want. For instance, you could return a single row using SingleOrDefault. The onus is on you to tell the query which one you want, hence you use Return matchingLogin.ToList().

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        S 1 Reply Last reply
        0
        • P Pete OHanlon

          As Michael said, you need to do:Return matchingLogin.ToList(). The reason you have to do this is because Linq uses a technique called deferred execution (please Google and have a read about this feature), which means that matchingLogin is actually IQueryable(Of tbl_User). If you think about this, it makes a lot of sense because there are so many different operations that you could perform here on your select that you need to tell it which one you want. For instance, you could return a single row using SingleOrDefault. The onus is on you to tell the query which one you want, hence you use Return matchingLogin.ToList().

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          S Offline
          S Offline
          sunil n cs
          wrote on last edited by
          #7

          First of all i would like to thank u guys for the support u have been providing. Now as i mentioned in my last reply i am using deferred execution i.e :Return matchingLogin.ToList()...this will work fine for the query Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _ Select Login Return matchingLogin.ToList() But when i use column names in select like this one Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _ Select Login.UserName, Login.Password Return matchingLogin.ToList() it gives me a error in blue underline for matchingLogin.ToList() and when i move the mouse on top it, it says Value of type 'System.Collections.Generic.List(Of )' cannot be converted to 'System.Collections.Generic.List(Of E_Learning.Web.tbl_User)'. So to remove this error i have removed .Tolist() from Return

          P 1 Reply Last reply
          0
          • S sunil n cs

            First of all i would like to thank u guys for the support u have been providing. Now as i mentioned in my last reply i am using deferred execution i.e :Return matchingLogin.ToList()...this will work fine for the query Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _ Select Login Return matchingLogin.ToList() But when i use column names in select like this one Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _ Select Login.UserName, Login.Password Return matchingLogin.ToList() it gives me a error in blue underline for matchingLogin.ToList() and when i move the mouse on top it, it says Value of type 'System.Collections.Generic.List(Of )' cannot be converted to 'System.Collections.Generic.List(Of E_Learning.Web.tbl_User)'. So to remove this error i have removed .Tolist() from Return

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #8

            Ah - sorry, I missed the bit where you picked out the smaller set of data. The problem you've got here, is that you've created an anonymous class in the Select. If you want to return the whole class, use Select Login instead, which selects the lot for you.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            S 1 Reply Last reply
            0
            • P Pete OHanlon

              Ah - sorry, I missed the bit where you picked out the smaller set of data. The problem you've got here, is that you've created an anonymous class in the Select. If you want to return the whole class, use Select Login instead, which selects the lot for you.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              S Offline
              S Offline
              sunil n cs
              wrote on last edited by
              #9

              I dont want to take the whole class, i want to take smaller set i.e only the required amount data from the database, is it that der is no option to take only required amount of data(Column selection option) here.

              P 1 Reply Last reply
              0
              • S sunil n cs

                I dont want to take the whole class, i want to take smaller set i.e only the required amount data from the database, is it that der is no option to take only required amount of data(Column selection option) here.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #10

                Then you'll have to create a class that matches these columns and return a list of that class instead. You'll use the select projection to fill that class.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                S 1 Reply Last reply
                0
                • P Pete OHanlon

                  Then you'll have to create a class that matches these columns and return a list of that class instead. You'll use the select projection to fill that class.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  S Offline
                  S Offline
                  sunil n cs
                  wrote on last edited by
                  #11

                  Thanks a lot, if possible can u plz give me an example on how to achieve it.

                  P 1 Reply Last reply
                  0
                  • S sunil n cs

                    Thanks a lot, if possible can u plz give me an example on how to achieve it.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #12

                    Bear in mind that I'm a C# developer (not a VB.NET one), so I may not get the syntax 100% correct, but I should imagine that this will work:

                    Public Function GetLoginName(ByVal strLogin As String) As List(Of UserSubset)
                    Implements IService1.GetLoginName
                    Dim db As New DataClasses1DataContext
                    Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _
                    Select New UserSubset With {.Name = Login.UserName, .Password = Login.Password }
                    Return matchingLogin.ToList
                    End Function

                    You will need to create a class called UserSubset that contains the properties Name and Password. That should be good enough to get you started.

                    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    S 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Bear in mind that I'm a C# developer (not a VB.NET one), so I may not get the syntax 100% correct, but I should imagine that this will work:

                      Public Function GetLoginName(ByVal strLogin As String) As List(Of UserSubset)
                      Implements IService1.GetLoginName
                      Dim db As New DataClasses1DataContext
                      Dim matchingLogin = From Login In db.tbl_Users Where Login.UserName.StartsWith(strLogin) _
                      Select New UserSubset With {.Name = Login.UserName, .Password = Login.Password }
                      Return matchingLogin.ToList
                      End Function

                      You will need to create a class called UserSubset that contains the properties Name and Password. That should be good enough to get you started.

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      S Offline
                      S Offline
                      sunil n cs
                      wrote on last edited by
                      #13

                      Thanks Pete O'Hanlon, i will try this out.:)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups