static void Main() { DataClasses1DataContext db = new DataClasses1DataContext(); string filePath = Environment.CurrentDirectory + @"\linq5.txt"; System.IO.TextWriter log = System.IO.File.CreateText(filePath); //指定该datacontext的log文件路径,用以记录查询过程中的sql语句log db.Log = log; //制定log输出路径为控制台输出 //db.Log = Console.Out; var obj = from m in db.test select new { m.ID, m.name }; obj.ToList();//必须是sql执行后才能记入log文件中 log.Close();