Click here to Skip to main content
14,668,804 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone,


I want to be able assign data retrieved from database to a variable using LINQ, i can do this using SqlDataReader:

e.g.

string name="", dept="",matNo="";
SqlDataReader rdr;

while(rdr.Read())
{
  name=["name"].ToString();
  dept =["dept"].ToString();
  matNo = ["matNo"].ToString();
}


How do i implement this using LINQ. Thanks in advance.
Posted
Comments
Wes Aday 23-Jul-14 9:22am
   
http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C
Uwakpeter 23-Jul-14 9:48am
   
What i can find from the link you posted is how to set field aliases, i meant how to be able to assign values to variables.
Wes Aday 23-Jul-14 11:10am
   
Then you either went to the wrong URL or you did not read the article.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

try this

private Team GetTeam(int id)

{

return (from t in db.Team where t.TeamId == id select t).SingleOrDefault();
}



private void AssignTeamName(Team team)

{

string teamName;



if (team != null)

teamName = team.TeamName;

}
   
Comments
Uwakpeter 24-Jul-14 6:03am
   
How do i call these two methods on pageload? Thanks
Uwakpeter 24-Jul-14 6:19am
   
Thanks, it works, i have been able to call it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100