Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
2.00/5 (1 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.

C#
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
[no name] 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.
[no name] 23-Jul-14 11:10am    
Then you either went to the wrong URL or you did not read the article.

1 solution

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;

}
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900