How to debug a LINQ Statement

后端 未结 9 1110
自闭症患者
自闭症患者 2020-12-04 23:47

I have a Linq to objects statement

 var confirm = from l in lines.Lines 
 where (l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber         


        
相关标签:
9条回答
  • 2020-12-05 00:31

    It is possible to step inside the LINQ expression without setting any temporary breakpoints. You need to step into the function which evaluates the LINQ expression, e.g.:

    var confirm = from l in lines.Lines 
                  where (l.LineNumber == startline.LineNumber)
                        || (l.LineNumber == endline.LineNumber) 
                  select l;
    
     confirm.ToArray(); // Press F11 ("Step into") when you reach this statement
    
     foreach(var o in q) // Press F11 when "in" keyword is highlighted as "next statement"
        // ...
    
    0 讨论(0)
  • 2020-12-05 00:36

    From the looks of the error I would suggest you take a look at line.Lines and make sure its enumerator is implemented properly. I think it's returning a null when it shouldn't.

    Oh and just make sure the line and line.Lines objects aren't null or returning nulls as well.

    0 讨论(0)
  • 2020-12-05 00:39

    Yes it is indeed possible to pause execution midway through a linq query.

    Convert your linq to query style using lambda expressions and insert a Select statement that returns itself somewhere after the point in the linq that you want to debug. Some sample code will make it clearer -

            var query = dataset.Tables[0].AsEnumerable()
                .Where (i=> i.Field<string>("Project").Contains("070932.01"))
     //         .Select(i =>
     //         {return i;}
     //           )
                .Select (i=>i.Field<string>("City"));
    

    Then uncomment the commented lines. Make sure the {return i;} is on its own line and insert a debug point there. You can put this select at any point in your long, complicated linq query.

    0 讨论(0)
提交回复
热议问题