Migration from Datatable to Linq to Sql

In the past, I have used dynamic sql and datatable to get data from a database.

For example:

Public shared function GetUsersByUsername(byval username as string) as datatable

dim strSQL as string="select * from

Users where Username= " & username

return dbClass.datatable(strSQL) 

end function

      

And I could use the following data:

Dim Email as string = GetUsersByUsername("mavera").rows(0).items("email")`

      

or

datagrid1.datasource=GetUsersByUsername("mavera")

datagrid1.databind()

      

And now I want to use linq to sql for this. I can write a query with linq, but I cannot use it as datatable. What should be my new use?

+1


source to share


1 answer


You have to get rid of GetUsersByName () completely, because you can do it in one line. You will also have to change how you receive things like the user's email. Thus, GetUsersByName () will be rewritten as follows:

dc.Users.Where(Function(u) u.Username = username);

      

and your email destination statement will be written as:



Dim Email as string = users.First().Email;

      

Forgive me if my VB syntax is disabled. I never use it again ...

+2


source







All Articles