Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq to sql - loop through table data and set value

Tags:

c#

linq-to-sql

I have a table "Users".. it has a column "ShowData"

using linq-sql how can I loop through every user and set ShowData to false for each user..

thanks

like image 231
raklos Avatar asked Sep 23 '26 07:09

raklos


2 Answers

Create a linq to sql classes designer file. Then drop the Users table on to the designer surface.

using (TheDataContext dc = new TheDataContext)
{
  //pull all users into memory, not recommended for large tables.
  List<User> users = dc.Users.ToList();

  foreach(User theUser in users)
  {
    theUser.ShowData = false;
  }
  //send all these property changes back to the database.
  dc.SubmitChanges();
}

And to guard myself against downvoting:

using (TheDataContext dc = new TheDataContext())
{
  dc.ExecuteCommand("UPDATE Users SET ShowData = 0");
}
like image 83
Amy B Avatar answered Sep 26 '26 01:09

Amy B


Create a stored procedure

CREATE PROCEDURE HideData 
AS
update Users set ShowData = 0

Using your server explorer dropdown this procedure in your Dbml

And in your function:

using (CustomDataContext context= new CustomDataContext())
{
  context.HideData();
}
like image 38
Gregoire Avatar answered Sep 26 '26 00:09

Gregoire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!