开发者

LINQ to ADO.NET Entity Data Modelassign value to object

开发者 https://www.devze.com 2023-02-05 11:45 出处:网络
Can I assign values to user object without looping in query result? I don\'t need looping there will be only one or no return from the query.

Can I assign values to user object without looping in query result? I don't need looping there will be only one or no return from the query.

protected user GetU开发者_StackOverflow社区ser(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;
    }
}


I guess you are looking for:

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

精彩评论

暂无评论...
验证码 换一张
取 消