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.