我可以在没有循环查询结果的情况下为用户对象赋值吗?我不需要循环,只有一个或不返回查询。
protected user GetUser(user u, string name, string password)
{
using (DBEntities db = new DBEntities())
{
var selectedUser = from p in db.users
where p.name == name && p.pass == password
select p;
if (selectedUser.Count() == 1)
{
foreach (user us in selectedUser)
{
u.id = us.id;
u.name = us.name;
u.pass = us.pass;
}
}
return u;
}
}
答案 0 :(得分:0)
我猜你在找:
using (var db = new DBEntities())
{
return db.users.SingleOrDefault(u => u.name == name && u.pass == password);
}