如何使用linq检索记录并将其分配给变量 [英] how to retrieve and assign record to a variable using linq

查看:80
本文介绍了如何使用linq检索记录并将其分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我希望能够使用LINQ将从数据库检索到的数据分配给变量,我可以这样做使用SqlDataReader:



例如



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();
}





如何使用LINQ实现此功能。在此先感谢。



How do i implement this using LINQ. Thanks in advance.

推荐答案

试试这个



私人团队GetTeam(int id)



{



返回(从db.Team中的t,其中t.TeamId == id select t).SingleOrDefault( );

}







private void AssignTeamName(团队团队)



{



string teamName;







if(team!= null)



teamName = team.TeamName;



}
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;

}


这篇关于如何使用linq检索记录并将其分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆