Datareader from LINQ Query for SqlBulkCopy

anonymous-types c# datareader linq sqlbulkcopy

Question

I'm using this library:

Link to EntityDataReader

to put some linq query results from a xml parse to a database with a sqlbulkcopy which us es a datareader to put the data directly without using datatables or such data structures.

I am not able to implement it.

This is the code I wrote so far without any success:

XDocument xdoc = XDocument.Parse(str_XmlToParse);

            //ORDINI

            IEnumerable<XDocument> xdod;

            var orders = from c in xdoc.Descendants("ordersdata").AsEnumerable().Descendants("order").AsEnumerable()
                         select new
                         {
                             Client_ID = (string)c.Element("Client").Element("ID"),
                             Doc_ID = (string)c.Element("ord_ID"),
                             Doc_data = (DateTime)c.Element("ord_datetime"),
                         };
      IDataReader dr = orders.AsDataReader();

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                using (SqlTransaction tran = con.BeginTransaction())
                {
                    var newOrders =
                            from i in Enumerable.Range(0, totalToSend)
                            select new Order
                            {
                                customer_name = "Customer " + i % 100,
                                quantity = i % 9,
                                order_id = i,
                                order_entry_date = DateTime.Now
                            };

                    SqlBulkCopy bc = new SqlBulkCopy(con,
                      SqlBulkCopyOptions.CheckConstraints |
                      SqlBulkCopyOptions.FireTriggers |
                      SqlBulkCopyOptions.KeepNulls, tran);

                    bc.BatchSize = 1000;
                    bc.DestinationTableName = "order_queue";
                    bc.WriteToServer(newOrders.AsDataReader());

                    tran.Commit();
                }
                con.Close();

which is mostly a copy of what is in the examples of that library.

I am not even understanding correctly what's going on with all their weird castings, but I need it to work. (believe me I tried to understand)

I keep getting this error:

Error 1 'System.Collections.Generic.IEnumerable AnonymousType#1>' does not contain a definition for 'AsDataReader' and no extension method 'AsDataReader' accepting a first argument of type 'System.Collections.Generic.IEnumerable AnonymousType#1>' could be found (are you missing a using directive or an assembly reference?)

What is it? How do I fix it?

Thanks

Accepted Answer

Well, basically the error say that you cannot apply the method "AsDataReader" with parameter an "IEnumerable" object because there is not such a method with this definition. If it might help, try to add in the references the DLL "System.Data.Entity" and System.Data.EntityClient



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow