mass delete/update objects without having to retrieve them first?

Lee Conlin asked on July 12, 2016 11:08

Is there some way in the Kentico API to mass delete/update objects without having to retrieve them first?

Similar to how EntityFramework Extended works?

Example Delete

UserInfoProvider.DeleteUsers().Where(x => x.UserEnabled == true);

This would build a SQL DELETE statement under the hood without any SELECT required?

Example Update

var userInforPartial = new UserInfo { UserEnabled = false };
UserInfoProvider.UpdateUsers(userInfoPartial).Where(x => x.UserIsHidden == true);

This would craft an UPDATE SQL statement to disable all hidden users.

Trevor: Your answer below still relies on making SELECT calls to the database before iterating the collection of retrieved objects. I'm looking for a way of telling Kentico to create UPDATE/DELETE SQL commands without having to retrieve the relevant object first.

In many cases, you just need to set a value on multiple records ... you know the record ID's and the value you want to set, so why add the extra overhead of a SELECT followed by an UPDATE when you could just do a straight UPDATE X SET Y=Z WHERE ID IN (1,2,3,4).

Recent Answers

Trevor Fayas answered on July 12, 2016 15:05

You may be able to look into the Linq "ForEach" command, i haven't tested but it would look like

UserInfoProvider.GetUsers().WhereEquals("UserIsHidden", true).ForEach(x => x.UserEnabled = false; x.Update());

This may or may not work though, sometimes it throws a fit modifying a listing, in which case just do a small foreach.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.