Bulk update with LINQ based on list [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores information of all the contacts that were added for sending SMS campaign.
Every SMS is associated with campaign. campaignid is used in the record for maintaining that.
I receive the sent mobile_numbers and campaign_id from service provider once the blast is done for campaign.
Now I need to iterate smscontacts table and do the following.
First Get all the records having campaign_id . Update the table by updating column issent as 1 for all the contacts that have mobile number in mobile_numbers list sent by provider after blast.
Edit:
Find rows that have column value for mobile number column in list mobile_numbers and update other columns value called for these records.

Assuming your MobileNumber property is a string and it is null, empty string or white space when it's not set, do the following.
// Get contacts that have a mobile number
IQueryable<Contact> contactsWithANumber = context.Contacts.Select(x => !string.IsNullOrWhiteSpace(x.MobileNumber));
// foreach
foreach(Contact c in contactsWithANumber)
{
c.IsSent = 1;
}
// .ForEach()
contactsWithANumber.ForEach(c => c.IsSent = 1);
// Update in db.
context.SaveChanges();

You can use ExecuteCommand to update multiple records:
yourContext.ExecuteCommand("UPDATE YourTable SET issent= 1 WHERE YourCondition");
For information see DataContext.ExecuteCommand Method

Related

Is there a way to reserve a variable used by the operation in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this issue in my c# MVC App where i have a receipt and this receipt has a receipt number and i select the last receipt number inserted in the database and add 1 to it, the problem appears when 2 users submitting the receipt at the same time. What happens is that they both get the same receipt number and they both insert into the database, unfortunately one is inserted and the other gives exception and rollback the whole operation. So my question is there a way to prevent this from happening, in other words reserve a receipt number taken by user, so it won’t be used by the other user.
Thanks in advance.
If using sql server you should probably use an IDENTITY column. Another option would be to use a GUID as the key.
So you need to reset the receipt number for each new year? You could make a separate call to get the receipt number first without doing inserts into other tables. This would reserve a number and return immediately. You could use an identity column for this, either a separate table per year or reset the identity value and truncate at the start of each new year. Or you could have a table for Ids with Year and ReceiptNumber as the PK and insert a new row with incrementing receipt number by one.

Generate variables from SQL database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I work on a project where I gather data from an SQL server.
I need to read 2 columns out of the database. The amount of rows can differ each time.
What I want to do is create variables modular to the amount of rows it has read.
An example:
I have 2 columns with the names: Name, Status.
In this table I have 10 rows. What I want my code to do is when it reads the data, it needs to create variables with the names from the Column Name and with the value of the column status.
So that would mean that when my code is done I have 10 new variables with the Names from Column: Name. And the values of those variables are the values from the column: Status.
Is there a proper way to do this?
Edit here is my test code:
while (reader.Read())
{
string F_Name = reader.GetString(0);
string status = reader.GetString(1);
list.Add(F_Name);
list.Add(status);
}
You can try using a dictionary like this:
var dict = new Dictionary<string, string>();
foreach (var row in rowsFromDb)
{
string name = row["Name"].ToString();
string status = row["Status"].ToString();
dict.Add(name, status);
}
A Dictionary is a data structure that could have many entries, and each entry has an index and a value, so they are linked (as you want).
And you can access the values of a dictionary this way:
string status = dict["VariableName"];

Result table more retain single array how can [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi sp returns 2 tables set record how to marge result set.
Result set table 1:
Result set table 2:
dtAll = new DataTable();
...
dtAll.Merge(dtOne);
dtAll.Merge(dtTwo);
dtAll.Merge(dtThree);
...
and so on.
This technique is useful in a loop where you want to iteratively merge data tables:
DataTable dtAllCountries = new DataTable();
foreach(String strCountry in listCountries)
{
DataTable dtCountry = getData(strCountry); //Some function that returns a data table
dtAllCountries.Merge(dtCountry);
}
If iam understanding you correctly
You wish to merge 2 identical data tables
dataTable.Merge(dataTable1);

how to add instantly made rows key to another table.? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.

Mysql and c# updating [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I currently have a mysql data table that fills up when people complete a form. I then have my visual studio c# bot pull this data and do what I want. The table numbers the table using integers that increase by 1 each time. My bot looks in the table for id 1 and adds it.
The problem I have is that once my bot completes what someone wants and deletes the data, it does not recognize to go onto the next one since that id is not 1 and something else, like 2. I tried to have my c# bot add 1 to a variable each time and then have that variable represent the id so that every time the bot completed something it would go onto the next id. The problem with this was that every time the bot completed with one person the variable was reset back to 1. I am stuck now and can't figure out what to do. Any help would be great.
This is currently the string I use "select name from names where id = 1". If I can somehow instead of having the id = 1 and have it look for the next lowest id in my table then that would work.
Edit: Actually I think I figured out how to have the last added id be used. I will post it if I find it.
In MySQL, if you want the lowest ID then you could do something like this:
SELECT
Name
FROM
Names
ORDER BY ID
Limit 1
Is that what you are looking for?

Categories