Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectReader.Create() additional parameter request #77

Open
bitofvg opened this issue Jul 22, 2019 · 0 comments
Open

ObjectReader.Create() additional parameter request #77

bitofvg opened this issue Jul 22, 2019 · 0 comments

Comments

@bitofvg
Copy link

bitofvg commented Jul 22, 2019

Would be nice to have something like :

ObjectReader.Create(orderDetails, fields, valuesToForce)

To be used in this way:

 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") });
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant