Search code examples
c#linqvisual-studio-2010debugginglinqpad

Debugging Visual Studio Linq query in LinqPad


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?


Solution

  • 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