views:

40

answers:

1

Hi

Looking for a bit of advice on this. I have a fairly robust .net web app where up till now ive only had to deal with records (in a sql server database) one at a time. I now have a requirement to do a batch update, of probably around 100 - 200 records at a time. I prefer using LINQ for querying, I know its not the best but just through personal choice I suppose. What would be the best way to approach this?

thanks

DD

A: 

If you want to update a set of records using a single SQL statement, you won't be able to do that with LINQ. You can certainly do a group of updates together, but it will be one statement per update using standard LINQ. If you've already got a LINQ connection open, I'd suggest constructing a SQL command using that, enlisting it in the same transaction as any queries you've started with LINQ if necessary.

tvanfosson
Its basically a gridview where once populated (either by LINQ or a sql datasource) i want to say, everything in that gridview, make FieldA=0, or something to that effect.
DarkWinter
If you want simple, just update every item in the collection that you've obtained via LINQ and do a SubmitChanges -- that will generate a statement per item being updated. If you want efficient, then construct a SqlCommand, probably using something like `update mytable set FieldA = 0 where id in (...)` with the in clause being constructed from the ids in the retrieved collection. You can either create a new connection or reuse the one on an open data context if available.
tvanfosson
got it, thanks for the help
DarkWinter