LINQ
-
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
-
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
Are you writing this code in Webservice (not Silverlight)? Then, it is not probably Silverlight problem. Never mind, we can still help. What error are you getting now? What is "strLogin" or What do you have in "db.tbl_Users"?
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders
-
Are you writing this code in Webservice (not Silverlight)? Then, it is not probably Silverlight problem. Never mind, we can still help. What error are you getting now? What is "strLogin" or What do you have in "db.tbl_Users"?
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders
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.
-
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.
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
-
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
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)'.
-
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
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 actuallyIQueryable(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 useReturn 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.
-
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 actuallyIQueryable(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 useReturn 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.
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
-
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
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, useSelect 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.
-
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, useSelect 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.
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.
-
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.
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.
-
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.
Thanks a lot, if possible can u plz give me an example on how to achieve it.
-
Thanks a lot, if possible can u plz give me an example on how to achieve it.
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 FunctionYou will need to create a class called UserSubset that contains the properties
Name
andPassword
. 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.
-
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 FunctionYou will need to create a class called UserSubset that contains the properties
Name
andPassword
. 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.
Thanks Pete O'Hanlon, i will try this out.:)