Seems you are calling the query a few times with id
, so let's just call it only once with all of them.
(from x in db.tblX where x.x_id == my_id && ids.Contains(x.x_id) select x)
There you go, now IEnumerable
's Contains
function allows you to look up the id in a list of ids
you have made in advance. I usually prefer to use LINQ methods, as simple LINQ expressions tend to be more complex than they should be:
db.tblX.Where(x => x.x_id == my_id && ids.Contains(x.x_id))
We can now use a plain foreach
as suggested in comments, such that we don't have side effects:
var ids = rlist.Where(x => x.IndexOf("_") != -1).Select(x => int.Parse(x.Split('_')[1]));
var items = db.tblX.Where(x => x.x_id == my_id && ids.Contains(x.x_id));
foreach (var item in items)
item.order = someNumber;
db.SaveChanges();
Done.
x.x_id
to bothmy_id
andid
? Does this code compile? Does it work as intended? Did you try to improve it by yourself before putting it on our collective table? – ANeves Nov 19 '12 at 9:03