0

I have a very large Visual Studio project and am in the process of debugging a complicated Linq query. The program gets it's data from a stored procedure in SQL, converts it to data tables and then does some background magic on it.

Somehow, all the questions I find here involve how to debug a Linqpad query in visual studio, not the other way round. Is there a short and easy way to serialize an EnumerableRowCollection in Visual Studio, and then deserialize it in LinqPad so I can play around with it in LinqPad? Or maybe there is another (better) way to debug?

So let's say I have this:

 var table = processManager.getDataTable();

var filteredRows = from row in table.AsEnumerable()
           where (row.Field<DateTime>("DateFrom") <= currentDate &&
                   row.Field<DateTime>("DateTo") >= currentDate)
                     select new
                     {
                        ....
                     };

The first line (processManager.getDataTable()) MUST run in visual studio. How can I debug in LinqPad?

d219
  • 2,707
  • 5
  • 31
  • 36
David
  • 15,652
  • 26
  • 115
  • 156
  • Have you tried creating the LinqPad item as a C# program instead of expression or statement? I often use that as I can include other items, data objects etc that I couldn't otherwise use. You just use results.Dump() to display the items – Andrew Mar 05 '14 at 12:54
  • Why don't you show us the code, which you can't run in Linqpad and why, so we can see what the actual problem is? – Marco Mar 05 '14 at 12:57
  • @serv There's a method which returns a datatable with 2000 rows. It's very complicated and works using dependency upon dependency and it's not a method which can be easily isolated. Maybe I can serialize the datatable to a string and deserialize it in LinqPad? – David Mar 05 '14 at 13:12
  • This makes it somewhat clearer. Let me scratch something up for you. – Marco Mar 05 '14 at 13:14

2 Answers2

1

Based on your comment, you essentially want to export the datatable from your solution and import it into Linqpad for further processing.

The easiest solution would be to export it into a csv - file. Let's take "vc 74"s solution posted here for the export:

StringBuilder sb = new StringBuilder(); 

//assuming processManager.getDataTable() returns a DataTable object
IEnumerable<string> columnNames = processManager.getDataTable().Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

Now that we have a csv file, lets reimport it in Linqpad:

//Where condition is optional; ensures no blank lines are processed
//to prevent index out of bounds error on Split
var csvData = from row in File.ReadLines(@"path/to/csv/file").Where(arg => !string.IsNullOrWhiteSpace(arg) && arg.Length > 0).AsEnumerable()
    //Delimiter
let column = row.Split(';')
select new 
{
    Prop1 = column[0],
    Prop2 = column[1],
    Prop3 = column[2],
    Prop4 = column[3],
    Prop5 = column[4]
};

You can also strongly type it, by defining all columns in a row as a seperate class in Linqpad and then call select new myClass { ... }.

Kind regards

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124
0

There is an extension that once installed and enabled on a solution, export your debugging variables to LINQPad directly. Check it out, it's called LINQBridgeVS:

It basically maps a custom debugger visualizer to all of the public classes and structs of a solution. So when you run your app, the magnifier glass on the data tip is available on almost any type:

Magnifying glass

Disclaimer: I'm the author of the extension.

codingadventures
  • 2,924
  • 2
  • 19
  • 36