LINQ to ADO.NET实体数据Modelass将值分配给对象

时间:2011-01-18 12:28:49

标签: linq entity-framework ado.net

我可以在没有循环查询结果的情况下为用户对象赋值吗?我不需要循环,只有一个或不返回查询。

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

1 个答案:

答案 0 :(得分:0)

我猜你在找:

using (var db = new DBEntities())
{   
  return db.users.SingleOrDefault(u => u.name == name && u.pass == password);
}