Efficient way to update a column from a table using LinqSql? - c#

In Sql, for example I would do:
UPDATE MyTable SET MyColum='value' WHERE Id=132
How would be the equivalent in a EF Code First database?
UPDATE:
Seeing the responses, I need to clarify my question. I am looking for an efficient way to update one column. If I use Single() or any similar function, the performance is very poor for two reasons: 1) There are 2 SQL statements, one for SELECT, and one for UPDATE, 2) The Single function retrieves all columns.
UPDATE MyTable SET MyColum='value' WHERE Id=132
The above sentence is efficient because it is only one transaction and no values are sent to the client from the server. I would like which would be the equivalent sentence in Linq Sql.

SingleOrDefault would return the object if exists in the db, or null otherwise:
var row = context.MyTable.SingleOrDefault(x => x.id == 132);
if(row != null) {
row.MyColumn = "Value";
context.SaveChanges();
}

I think it is not possible with one transaction.you need first to check that row you want to update is in your table or not
using (MyEntities me=new MyEntities())
{
if( (from t in me.MyTables where mt.Id == 132 select t).Any())
{
MyTable mt= (from t in me.MyTables where mt.Id == 132 select t).Single();
mt.MyColumn= "Value";
me.SaveChanges();
}
}

Related

get and post table type using pqxx

Good day, friends. I'm using pqxx and I have some questions.
1. I have two tables:
table1 (table1_id integer) and table2 (table1_id integer, another_id integer) with relation one-to-many.
How I can easy get information in view like: table1_id, vector another_ids?
Now I use serializtion in script (string concat into "%d %d %d...") and deserialization in my c++ code.
Also I need insert value into table1. And how I can do this in one transaction?
2. I call stored procedure like
t.exec("SELECT * FROM my_proc(some_argument)");
May be exists any way to do this like in c#?
Thank you very much!
So, may be it can help someone.
In first case I find and use two ways:
1. Group concat in sql function and deserialization in c++. It fast if table2 has only table1_id and another integer.
2. I call two function: get_table1() and get_table2() with order by table1_id. And then with two pointers I create array of table1:
std::vector<Table1> table1Array;
auto ap = wrk.prepared(GetTable1FuncName).exec();
auto aps = wrk.prepared(GetTable2FuncName).exec();
auto pos = aps.begin();
for (auto row = ap.begin(); row != ap.end(); ++row) {
std::vector<Table2> table2Array;
while (pos != aps.end()
&& row["table1_id"].as(int()) == pos["table1_id"].as(int())) {
table2Array.push_back(Table2(pos["first_id"].as(int()),
pos["second_string"].as(std::string())));
++pos;
}
Table1 tb1(row["table1_id"].as(int()), row["column2"].as(int()),
row["column3"].as(int()), row["column4"].as(int()),
table2Array);
table1Array.push_back(tb1);
}
May be it is not pretty, but it's working.
Insert into database I write for one element. Firstly insert into Table1, and after several lines into Table2. After call pqxx::work.commit().
In second case Not, doesn't exists. Also remember, function always return 1 line! Be careful!

How do I use LINQ to update a datatable with a SqlDataReader?

I am trying to merge data from two separate queries using C#. The data is located on separate servers or I would just combine the queries. I want to update the data in one of the columns of the first data set with the data in one of the columns of the second data set, joining on a different column.
Here is what I have so far:
ds.Tables[3].Columns[2].ReadOnly = false;
List<object> table = new List<object>();
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().Where(s => r[3] == s[0])).ToList();
The ToList() is just for debugging. To summarize, ds.Tables[3].Rows[2] is the column I want to update. ds.Tables[3].Rows[3] contains the key I want to join to.
In the reader, the first column contains the matching key to ds.Tables[3].Rows[3] and the second column contains the data with which I want to update ds.Tables[3].Rows[2].
The error I keep getting is
Unable to cast object of type 'WhereEnumerableIterator1[System.Data.IDataRecord]' to type 'System.IConvertible'.Couldn't store <System.Linq.Enumerable+WhereEnumerableIterator1[System.Data.IDataRecord]> in Quoting Dealers Column. Expected type is Int32.
Where am I going wrong with my LINQ?
EDIT:
I updated the line where the updating is happening
table = ds.Tables[3].AsEnumerable().Select(r => r[2] = reader.AsEnumerable().First(s => r[3] == s[0])[1]).ToList();
but now I keep getting
Sequence contains no matching element
For the record, the sequence does contain a matching element.
You can use the following sample to achieve the join and update operation. Let's suppose there are two Datatables:
tbl1:
tbl2:
Joining two tables and updating the value of column "name1" of tbl1 from column "name2" of tbl2.
public DataTable JoinAndUpdate(DataTable tbl1, DataTable tbl2)
{
// for demo purpose I have created a clone of tbl1.
// you can define a custom schema, if needed.
DataTable dtResult = tbl1.Clone();
var result = from dataRows1 in tbl1.AsEnumerable()
join dataRows2 in tbl2.AsEnumerable()
on dataRows1.Field<int>("ID") equals dataRows2.Field<int>("ID") into lj
from reader in lj
select new object[]
{
dataRows1.Field<int>("ID"), // ID from table 1
reader.Field<string>("name2"), // Updated column value from table 2
dataRows1.Field<int>("age")
// .. here comes the rest of the fields from table 1.
};
// Load the results in the table
result.ToList().ForEach(row => dtResult.LoadDataRow(row, false));
return dtResult;
}
Here's the result:
After considering what #DStanley said about LINQ, I abandoned it and went with a foreach statement. See code below:
ds.Tables[3].Columns[2].ReadOnly = false;
while (reader.Read())
{
foreach (DataRow item in ds.Tables[3].Rows)
{
if ((Guid)item[3] == reader.GetGuid(0))
{
item[2] = reader.GetInt32(1);
}
}
}

C# Find non matching values in DataTables

I'm struggling with the following problem:
There are 2 DataTables (SSFE and FE in my case).
FE will contain items that match with SSFE, but it will also contain values not present in SSFE.
For Example
SSFE 1,2,3,4,5,6,9,10
FE 1,2,3,4,5,6,7,8,9,10,11
The ouput I need is in this example : 7, 8, 11.
I'm using the following code to find items that do match:
DataSet set = new DataSet();
//wrap the tables in a DataSet.
set.Tables.Add(SSFEData);
set.Tables.Add(FEData);
//Creates a ForeignKey like Join between two tables.
//Table1 will be the parent. Table2 will be the child.
DataRelation relation = new DataRelation("IdJoin", SSFEData.Columns[0], FEData.Columns[0], false);
//Have the DataSet perform the join.
set.Relations.Add(relation);
//Loop through table1 without using LINQ.
for (int i = 0; i < SSFEData.Rows.Count; i++)
{
//If any rows in Table2 have the same Id as the current row in Table1
if (SSFEData.Rows[i].GetChildRows(relation).Length > 0)
{
SSFEData.Rows[i]["PackageError"] = SSFEData.Rows[i].GetChildRows(relation)[0][1];
SSFEData.Rows[i]["SaleError"] = SSFEData.Rows[i].GetChildRows(relation)[0][2];
}
}
There should be an trick to find these items that do not have an relation.
Any suggestion will be great!
Well, you could of course use a little bit of LINQ by turning the data tables into IEnumerables using the AsEnumerable()1 extension method.
I am using a few assumptions to illustrate this:
"id" is the column with an integer value relating rows in FEData and SSFEData.
"id" is the primary key column on both FEData and SSFEData.
Then this will return a list of rows from FEData that are not present in SSFEData:
var notInSSFEData = FEData.AsEnumerable()
.Where(x => SSFEData.Rows.Find((object)x.Field<int>("id")) == null)
.ToList();
If assumption 2 above does not hold (i.e. the "id" field is not the primary key), a slightly more elaborate query is required.
var notInSSFEData = FEData.AsEnumerable()
.Where(x1 => !SSFEData.AsEnumerable().Any(x2 => x2.Field<int>("id") == x1.Field<int>("id")))
.ToList();
1 this requires adding a reference to System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll).

Update multiple records in database

I am attempting to update multiple records in my DB, I have the below code, which I am using to update 1 item.
How do I update multiple?
abcProduct productUpdate = dc.abcProducts.Single(p => p.feedSubmitId == submissionId);
productUpdate.prodPublished = '1';
Try this
dc.abcProducts.Where(p => p.feedSubmitId == submissionId).ToList().ForEach(x=>x.prodPublished = '1');
var products = dc.abcProducts.Where(p => p.feedSubmitId == submissionId);
foreach(var product in products)
{
product.prodPublished = '1';
}
You can also write a stored procedure in your database.
CREATE PROCEDURE SetProdPublished
#submissionId int
#prodPublished int
AS
UPDATE AbcProducts SET prodPublished = #prodPublished
WHERE feedSubmitId = #submissionId
Then you drag that stored procedure into your DBML. You can then call that stored procedure.
dataContext.SetProdPublished(someSubmissionId, 1);
If you are updating more than a few rows, this will be faster than updating them in a loop in code, and much faster if you're updating a lot of rows.

LINQ to Entities how to update a record

Okay, so I'm new to both EF and LINQ. I have figured out how to INSERT and DELETE but for some reason UPDATE seems to escape my grasp.
Here is a sample of my code:
EntityDB dataBase = new EntityDB();
Customer c = new Customer
{
Name = "Test",
Gender = "Male
};
dataBase.Customers.AddObject(c);
dataBase.SaveChanges();
The above creates and adds a record just fine.
Customer c = (from x in dataBase.Customers
where x.Name == "Test"
selext x).First();
dataBase.Customers.DeleteObject(c);
dataBase.SaveChanges();
The above effectively deletes the specified record.
Now how do I update? I can't seem to find an "UpdateObject()" method on the entity collection.
Just modify one of the returned entities:
Customer c = (from x in dataBase.Customers
where x.Name == "Test"
select x).First();
c.Name = "New Name";
dataBase.SaveChanges();
Note, you can only update an entity (something that extends EntityObject, not something that you have projected using something like select new CustomObject{Name = x.Name}
//for update
(from x in dataBase.Customers
where x.Name == "Test"
select x).ToList().ForEach(xx => xx.Name="New Name");
//for delete
dataBase.Customers.RemoveAll(x=>x.Name=="Name");
They both track your changes to the collection, just call the SaveChanges() method that should update the DB.
In most cases #tster's answer will suffice. However, I had a scenario where I wanted to update a row without first retrieving it.
My situation is this: I've got a table where I want to "lock" a row so that only a single user at a time will be able to edit it in my app. I'm achieving this by saying
update items set status = 'in use', lastuser = #lastuser, lastupdate = #updatetime where ID = #rowtolock and #status = 'free'
The reason being, if I were to simply retrieve the row by ID, change the properties and then save, I could end up with two people accessing the same row simultaneously. This way, I simply send and update claiming this row as mine, then I try to retrieve the row which has the same properties I just updated with. If that row exists, great. If, for some reason it doesn't (someone else's "lock" command got there first), I simply return FALSE from my method.
I do this by using context.Database.ExecuteSqlCommand which accepts a string command and an array of parameters.
Just wanted to add this answer to point out that there will be scenarios in which retrieving a row, updating it, and saving it back to the DB won't suffice and that there are ways of running a straight update statement when necessary.

Categories