Friday, March 4, 2011

Migration From Datatable to Linq to Sql

In past I use dynamic sql and datatable to get data from database.

Such as :

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 this data such this:

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 to do that. I can write query with linq, but I can't use it like a datatable. What should my new using be done?

From stackoverflow
  • You should get rid of GetUsersByName() altogether, because you can do it in one line. You will also have to change how you get things like the user's email. So GetUsersByName() would be rewritten something like:

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

    and your email assignment statement would be written as:

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

    Forgive me if my VB syntax is off. I never use it anymore...

0 comments:

Post a Comment