帮助,简单的LINQ查询
问题描述:
我的表有以下几列:帮助,简单的LINQ查询
用户(用户名,组ID,用户名,用户类型,...)
我需要这样做:
SELECT GroupID
FROM Users
WHERE userID = @userID AND username = @username and usertype = @usertype.
我有我的DataContext准备好去,我的LINQ查询指导将是伟大的!
答
您应该下载LinqPad并使用它连接到您的数据源。这将真正帮助您开始使用LINQ。我还建议购买LinqPad的完整版($ 19)B/C智能感知可以真正帮助你在开始
答
int UserID = [code to get userID],
Usertype=[code to get usertype];
string Username=[code to get username];
from u in Users
where u.userID = UserID
&& username=Username
&& usertype=Usertype
select u.GrupID;
答
大约...
int ID = 1;
string uname = "whatever";
string utype = "whatever"
User myUser = (from u in Users where u.userID == ID && u.username == uname && u.usertype == utype select u).SingleOrDefault();
但是,是的,抢linqpad
答
var result = from u in users
where u.UserID == userID
&& u.UserName == userName
select u.GroupID
答
我不确定你是否想要一个结果或一个列表。我假设只有一个将被发现,因为你正在搜索一个用户ID。
string userID = "Your user ID here";
string userName = "Your username here";
string userType = "Your usertype here";
var groupID = (from user in dataContext.Users
where user.UserID == userID &&
user.UserName == userName &&
user.UserType == userType
select user.GroupID).FirstOrDefault();
如果你想要一个与你匹配的所有行的列表,可以在末尾做一个ToList()。
我还没有测试代码,所以它可能有一些错别字,我不确定你的变量名是从你的dbml中得到的。
@mrblah如果这个答案或另一个帮助你,你应该设置一个被接受的答案,以便其他人阅读这个问题可以找到帮助你的解决方案。 – Kelsey 2010-08-18 16:08:18