Linq to SQL - 选择新对象并执行更新
我有一个将数据选择为自定义类型的查询 -
UserData curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)
.Select( λ => new UserData { ID = λ.ID, UserName = λ.Login, isAdmin = λ.Admin, isTrusted = λ.Trusted, EMail = λ.E_mail ... });
//Yeah I know this isn't a technically correct usage of 'λ'
//It is just a convenient variable name. Please don't hit me.
当我获取用户信息时,我希望能够更新“LastIP”和“LastVisit”等信息,然后在密码正确且登录成功的情况下提交更改。
如果我理解正确,新对象已分离 - 因此如果我更改它并调用 dc.SubmitChanges(),则不会保存对其的更改。那么在这种情况下执行更新的最佳方法是什么?我是否需要再做一次选择并更改那里的数据,有没有办法可以将其内联到我的初始语句中?
感谢您的帮助!
我把它放在一起,想我会发布它,以防万一其他人需要内联解决方案 -
internal static class auth_extensions
{
public static IQueryable<User> Update(this IQueryable<User> data, Action<User> updateDelegate)
{
foreach (User cur in data)
{
updateDelegate(cur);
}
return data;
}
}
}
通过程序集中的扩展方法,查询变得 -
UserData curData = dc.Users
.Where(λ => (λ.Login == username) && λ.Active)
.Update(λ => {
λ.LastIP = HttpContext.Current.Request.UserHostAddress;
λ.lastVisit = DateTime.Now;
})
.Select(loadDelegate).SingleOrDefault();
话虽如此,cdonner 的建议非常好。
I have a query that selects data into a custom type-
UserData curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)
.Select( λ => new UserData { ID = λ.ID, UserName = λ.Login, isAdmin = λ.Admin, isTrusted = λ.Trusted, EMail = λ.E_mail ... });
//Yeah I know this isn't a technically correct usage of 'λ'
//It is just a convenient variable name. Please don't hit me.
When I get a user's information I'd like to be able to update information like 'LastIP' and 'LastVisit' then submit the changes if the password is correct and the log in succeeds.
If I understand correctly the new object is detatched- so changes to it will not be saved if I alter it and call dc.SubmitChanges(). So what is the best way to perform an update in this situation. Do I need to just do another select and alter the data there, is there a way I can inline it on my initial statement?
Thanks for the help!
I put this together, figured I'd post it just in case anyone else needed an inline solution-
internal static class auth_extensions
{
public static IQueryable<User> Update(this IQueryable<User> data, Action<User> updateDelegate)
{
foreach (User cur in data)
{
updateDelegate(cur);
}
return data;
}
}
}
With the extension method in the assembly the query becomes-
UserData curData = dc.Users
.Where(λ => (λ.Login == username) && λ.Active)
.Update(λ => {
λ.LastIP = HttpContext.Current.Request.UserHostAddress;
λ.lastVisit = DateTime.Now;
})
.Select(loadDelegate).SingleOrDefault();
That being said cdonner's suggestion is excellent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有更多上下文,您的意图就不清楚。您正在将用户属性从 User 对象复制到 UserData 对象中。为什么不直接编辑一个 User 对象呢?然后 SubmitChanges() 将自动保留您的更新。 IE
Without more context, your intention is not clear. You are copying user attributes from a User object into a UserData object. Why don't you just edit a User object? Then SubmitChanges() will automatically persist your updates. i.e.
这是一个简单的例子:
Here it is an simple example: