You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var table = new DataTable();
string[] fields = { "OrderId", "Price", "Category", "Manufacturer", "Discount" };
var valuesToForce = new { OrderId = 15, Discount = 30 };
var reader = ObjectReader.Create(orderDetails, fields, valuesToForce))
table.Load(reader);
When the datareader reads the values of fields OrderId or Discount, the value read is forced to the value contained in valuesToForce.
This is very useful with Dapper, after I wrote the Order record and get its Id (identity), normally I loose performance because I have to do the following before insert the order details:
orderDetails.ForEach(od => od.OrderId = OrderId);
with the valuesToForce parameter, I can do id immediately, I can insert the order details with something like:
public void InsertBulkOrderDetails(List<OrderDetail> orderDetails, int OrderIdToForce) {
using (var connection = new SqlConnection(@"bla bla bla....")) {
var table = new DataTable();
string[] fields = { "OrderId", "Price", "Category", "Manufacturer", "Discount" };
var valuesToForce = new { OrderId = OrderIdToForce, Discount = 30 };
using (var reader = ObjectReader.Create(orderDetails, fields, valuesToForce)) {
table.Load(reader);
}
var csFieldsList = string.Join(",", fields.Select(x => "[" + x + "]"));
var sql = @"INSERT OrderDetails(" + csFieldsList + @")
SELECT * FROM @data";
connection.Execute(sql,
new { @data = table.AsTableValuedParameter("dbo.OrderDetailsToInsert") });
}
}
The text was updated successfully, but these errors were encountered:
Would be nice to have something like :
ObjectReader.Create(orderDetails, fields, valuesToForce)
To be used in this way:
When the datareader reads the values of fields OrderId or Discount, the value read is forced to the value contained in valuesToForce.
This is very useful with Dapper, after I wrote the Order record and get its Id (identity), normally I loose performance because I have to do the following before insert the order details:
orderDetails.ForEach(od => od.OrderId = OrderId);
with the valuesToForce parameter, I can do id immediately, I can insert the order details with something like:
The text was updated successfully, but these errors were encountered: