是LINQifying我的代码值得一结束访问一个foreach变量? [英] Is LINQifying my code worth accessing a foreach variable in a closure?

查看:160
本文介绍了是LINQifying我的代码值得一结束访问一个foreach变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回想起昔日的盗版直播滚石乐队录音的称号,ReSharper的是更清晰,比我会永远定;因为我有它检查我的代码,它告诉我,这对于封:

Reminiscent of the title of a bootleg live Rolling Stones recording of yesteryear, Resharper is sharper than I'll ever be; as I had it inspect my code, it told me this regarding closures:

1)循环:

        foreach (var item in PlatypiIds)
        {
            var query = db.Table<Locations>().Where(l => l.PlatypusId == item).
                Where(l=> l.SentTimeUTC >= EarliestToShow).
                Where(l=> l.SentTimeUTC <= LatestToShow).
                OrderBy(l => l.SentTimeUTC);

            if (query != null)
            {
                foreach (var q in query)
                {
                    listLocs.Add(q);
                }
            }
        }

...可以转换成LINQ表达式:

...can be converted into LINQ expression:

listLocs.AddRange(from item in PlatypiIds select db.Table<Locations>().Where(l => l.PlatypusId == item).Where(l => l.SentTimeUTC >= EarliestToShow).Where(l => l.SentTimeUTC <= LatestToShow).OrderBy(l => l.SentTimeUTC) into query 
where query != null from q in query select q);"

...但随后ReSharper的后来告诉我关于新的和改进的代码:在封访问的foreach变量。当使用不同版本的编译器的编译可能有不同的行为。

...but then Resharper told me later regarding the "new and improved" code: "Access to foreach variable in closure. May have different behaviour when compiled with different versions of compiler"

那么,什么是与不同版本的编译器编译的的可能性?我的意思是,我不会去往回走,版本明智的,从VS2012到VS2010,例如... ???

So what are the possibilities of compiling with a different version of the compiler? I mean, I'm not going to go backwards, version-wise, from VS2012 to VS2010, for example...???

2)这些行:

            if (db != null)
                db.Insert(new PlatypiRequested()

...此代码的:使用(VAR分贝

...of this code:

    using (var db = new SQLiteConnection(SQLitePath))
    {
        db.CreateTable<PlatypiRequested>();

        db.RunInTransaction(() =>
        {
            if (db != null)
                db.Insert(new PlatypiRequested()
                              {
                                  PlatypusId = PlatypusId,
                                  PlatypusName = PlatypusName,
                                  InvitationSentLocal = invitationSentLocal
                              });
        });
    }



... ReSharper的告诉我,获得处置倒闭

...Resharper informs me, "Access to disposed closure"

这是什么意思,和我应该怎么办呢?

What does that mean, and what should I do about it?

推荐答案

你这里有两个不同的问题,一是LINQ VS的foreach,另一个是不同的情况。

You have two different issues here, one LINQ vs foreach, and the other is a different case.

至于ReSharper的通知您访问在封闭的foreach变量.. 当代码LINQified - 我从来没有把我的机会,并把它作为一个foreach循环。在大多数情况下,它也更可读性和可维护性,真的,缩短了代码不算多大不了的。

Regarding the ReSharper informing you of "Access to foreach variable in closure..." when the code is LINQified - I just never take my chances, and leave it as a foreach loop. In most cases it is also more readable and maintainable, and really, shortening the code isn't that much of a big deal.

对于第二种情况 - 你会需要减肥的使用语句,因为分贝对象将被过早地处理。您应该关闭,并在老派时尚处置它里面的 RunInTransaction lambda表达式,在它的结束。

Regarding the second case - you'll need to lose the using statement, since the db object will be disposed too soon. You should close and dispose it in the "old school fashion" INSIDE the RunInTransaction lambda expression, at the end of it.

这篇关于是LINQifying我的代码值得一结束访问一个foreach变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆