×

Loading...
Ad by
Ad by

ToList()或者ToArray()好象并不好使....

很早以前我就做过类似的试验,比如以下程序

static void Lab129()
{
string[] ss = new string[] {"kk","pppp","aaa" };
string[] result = ss.Where(s => s.Length > 2).ToArray();

Console.WriteLine("result");
}

如果你在最后一行 Console.WriteLine("result"); 处设置断点,试图察看result 的数据,因为Lazy operators 的原因系统显示没有 result 这个变量,必须要显示的access一下 result 才可以,如下

static void Lab129()
{
string[] ss = new string[] {"kk","pppp","aaa" };
string[] result = ss.Where(s => s.Length > 2).ToArray();

Console.WriteLine(result);
}

这也是我为什么调试的时候经常用Console.WriteLine的原因,很烦人,但是没有办法。
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 正在用LINQ的,或者正在学LINQ的,推荐一款小软件LINQPad,附加很多例子,很实用。如果你用LINQ to SQL,想分析LINQ最终生成的SQL代码,用LINQPad轻松得到
    • 谢谢大猫,看样子不错,明天就试试。
    • Don’t get any its advantage yet, I mean any LINQ query can be written in VS. Why use the separated tool?
      Should user copy & paste the queries to VS or what else? I get lost.
      • 如果学习Linq,这个工具比VS更直观、友好一些,就凭那么多.Net专业人士的 Recommendations 也是值得一看的。对于高手来说当然可以不屑一顾,^_^
        • In my personal experience, LINQ is pretty similar to SQL. There isn’t much complicated syntax. The complicated results can only be done by experiences rather than any tools.
          • Yes, for a complex SQL, there are many select groups, inner join, left join, join inside another join, case statements, change column to different name, etc. WRITING SQL is challenging and fun.
            • I agree. BTW, I was wondering when you become more and more technique oriented?
              • Funny question! Are you one of those tech guru guys who believe I am not technical oriented, and don't know VERSION CONTROL at all? What a Rolia world.
                • Too abashed. my level is only to use some very simple one like VSS. it's not funny at all.
                  • Are you using SVN in Eclipse, or Jdeveloper? Or !Ciao in Notes Domino? Version control is just an application of database and user /role control.
                    • Pay attention to what BLUE said if you reply to him, okay?
          • I think LINQ 's syntex is not much same as sql, for example, outer left/right join, group, select ... in sub-sql, and it makes not only design time headache, but difficult to debug(sql can be simply copy into studio management and run script).
            • Did I say that LINQ and SQL have same syntax?
              Of course they are pretty similar (comprehension query) but not same (lambda query is only similar in logic). If you use LINQ as exact same way as SQL, no doubt it causes you headache.

              1. outer join: In LINQ, the group join has similar function as outer join in SQL but not so powerful.

              2. group by function: More convenient (easily getting key value) but only group by one key rather than by several fields as that in SQL. If you really need group by multi-fields, you can concatenate multi-fields as a group key. And LINQ’s groupby result is individual group objects rather than individual records as SQL.

              3. Sub-query: Since most LINQ operators are lazy operators (execute until output result or run a greedy operator), you can use either in-line sub-query or separated sub-query to get exact same result and run in exact same behaviour. The following two queries are examples. They run in exact same behaviour. Apparently the separated sub-query is more readable.

              // in-line sub-query
              var q1 = ds.Tables[1].AsEnumerable().Where(r1 => ds.Tables[0].AsEnumerable().Where(r2 => r2.Field("City").Equals("Toronto")).Select(r2 => r2.Field("AccountID")).Contains(r1.Field("AccountID")));

              // seperated sub-query
              var q_sub = ds.Tables[0].AsEnumerable().Where(r => r.Field("City").Equals("Toronto")).Select(r => r.Field("AccountID"));

              var q2 = ds.Tables[1].AsEnumerable().Where(r => q_sub.Contains(r.Field("AccountID ")));
              • u r the man! the sub-query linq example looks useful to me, thanks.
              • 嗯,哥们提到到lazy operators确实令我很头疼,debug的时候经常不能hit设置的断点,看不到执行的步骤。俺经常的办法是用Console.WriteLine(解决,调试通过以后把Console.Write删掉。设置SubQuery是个好方法,就是太费脑子了.... ^_^
                • If executing time of query doesn’t matter, why don’t use greedy operators, e. g. .ToList<Object>() or .ToArray<Object>()?
                  • ToList()或者ToArray()好象并不好使....
                    很早以前我就做过类似的试验,比如以下程序

                    static void Lab129()
                    {
                    string[] ss = new string[] {"kk","pppp","aaa" };
                    string[] result = ss.Where(s => s.Length > 2).ToArray();

                    Console.WriteLine("result");
                    }

                    如果你在最后一行 Console.WriteLine("result"); 处设置断点,试图察看result 的数据,因为Lazy operators 的原因系统显示没有 result 这个变量,必须要显示的access一下 result 才可以,如下

                    static void Lab129()
                    {
                    string[] ss = new string[] {"kk","pppp","aaa" };
                    string[] result = ss.Where(s => s.Length > 2).ToArray();

                    Console.WriteLine(result);
                    }

                    这也是我为什么调试的时候经常用Console.WriteLine的原因,很烦人,但是没有办法。
                    • No any problem to hit BP, no matter using lazy or greedy opeartor!
                      Either following case to to hit bolded line. You might need reinstall your VS.


                      string[] ss = new string[] {"kk","pppp","aaa" };
                      string[] result = ss.Where(s => s.Length > 2).ToArray();
                      Console.WriteLine("result");



                      string[] ss = new string[] {"kk","pppp","aaa" };
                      var = ss.Where(s => s.Length > 2);
                      Console.WriteLine("result");
                      • Sorry,可能是我以前的语言表达不准确,我碰到的和Lazy Operator有关的问题是:
                        1)string[] ss = new string[] {"kk","pppp","aaa" };
                        2)string[] result = ss.Where(s => s.Length > 2).ToArray();
                        3)Console.WriteLine("result");

                        在第三行设置断点,VS当然能够捕捉到断点,这个时候我把鼠标移向第二行的 result,或者在 Immediate Window中打入“result”在回车想看看result变量的值,系统会提示你没有result这个变量,因为Lazy Oper的原因

                        所以我后来说我必须要“显示”的access 一下 result这个变量,这样才能在断点3)处看到result的值
                        这样,我把第三行变为
                        3)Console.WriteLine(result); // 注意result周围这次没有双引号"",意味着我要access一下变量result

                        如果这个时候在3)处设置断点,就会看到result里面的值

                        我的问题就是因为lazy oper我经常要在调试的时候用Console.WriteLine(来access我要查看的变量,很烦人,尤其在和Linq有关的变量很多的时候,而且找不到更好的方法
                        • Try Watch window: Debug -> QuickWatch…
                • Sub-query is not greedy operator. It doesn’t help you for the problem you mentioned.
        • BTW, PLINQ may have little more options and changes in order to improve performance. You might take more attention to it.
          • 嗯,刚刚下载了2008 6月的CPT版,大概看了一下,我的天哪!那性能提升不是一点半点,太可怕了,多核的威力..... 估计微软肯定会在.net 4.0中将集成这种Parallel Extensions。多谢兄台介绍
    • tried. Seems still need improve.
    • Great. For my daily work, I use some tools to debug or speed up. Some of them are free. Finding the right tool is a fun and rewarding process.