Linq to SQL - 选择新对象并执行更新

发布于 2024-08-05 03:48:08 字数 1336 浏览 5 评论 0原文

我有一个将数据选择为自定义类型的查询 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-08-12 03:48:08

如果没有更多上下文,您的意图就不清楚。您正在将用户属性从 User 对象复制到 UserData 对象中。为什么不直接编辑一个 User 对象呢?然后 SubmitChanges() 将自动保留您的更新。 IE

User curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)

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.

User curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)
隔纱相望 2024-08-12 03:48:08

这是一个简单的例子:

    using(DbContext dbContext = new DbContext())
    {
        UserData curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active).SingleOrDefault(); // You need(!) to use SingleOrDefault or FirstOrDefault
        if(curData != null) 
        {
            curData.LastIP = 'xx.xx.xx.xx';
            curData.LastVisit = DateTime.Now;
            dbContext.SaveChanges();
        }
    }

Here it is an simple example:

    using(DbContext dbContext = new DbContext())
    {
        UserData curData = dc.Users.Where(λ => (λ.Login == username) && λ.Active).SingleOrDefault(); // You need(!) to use SingleOrDefault or FirstOrDefault
        if(curData != null) 
        {
            curData.LastIP = 'xx.xx.xx.xx';
            curData.LastVisit = DateTime.Now;
            dbContext.SaveChanges();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文