称呼其他代码书呆子-嵌套循环的替代方法? [英] Calling fellow code nerds - Alternatives to Nested Loops?

查看:88
本文介绍了称呼其他代码书呆子-嵌套循环的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我(或可能是其他任何人)来说,拥有我需要迭代的对象列表然后与属性列表进行交互的情况并不少见.我使用嵌套循环,如下所示:

It is not uncommon for me (or likely anyone else) to have a list of objects I need to iterate through and then interact with a list of properties. I use a nested loop, like this:

IList<T> listOfObjects;
IList<TProperty> listOfProperties;

foreach (T dataObject in listOfObjects)
{
    foreach (TProperty property in listOfProperties)
    {
        //do something clever and extremely useful here
    }
}

这是针对此问题的经过时间和性能测试的模式吗?还是有一些性能更高,更优雅或更有趣的东西(当然仍然是可读性和可维护性的)?

Is this the time and performance tested pattern for this problem? Or is there something more performant, more elegant, or just plain fun (while still being readable and maintainable of course)?

上面的代码并没有让我微笑.有人可以帮我带来一些快乐吗?

The code above doesn't make me smile. Can someone please help bring some joy to my loop?

谢谢!

更新:我最积极地使用书呆子"一词.作为Wikipedia定义的一部分,它指的是一个热衷于从事智力活动的人".所谓代码书呆子",是指那些关心不断提高自己作为程序员的能力,寻找快速,可维护和美观的新颖,新颖,优雅的编码方式的人!他们为摆脱VB6感到高兴,并希望聪明的人批评他们的代码并帮助他们进行自我智能化. (注意:他们还喜欢制作以-ify结尾的新单词.)

Update: I use the term "nerd" in a most positive sense. As part of the wikipedia definition puts it "that refers to a person who passionately pursues intellectual activities". By "code nerd" I mean someone who is concerned about continually improving oneself as a programmer, finding new, novel, and elegant ways of coding that are fast, maintainable, and beautiful! They rejoice to move out of VB6 and want smart people to critique their code and help them smartify themselves. (Note: they also like to make new words that end in -ify).

最后的笔记:

感谢Dave R,Earwicker和TheSoftwareJedi将我送到Linq的路上.这只是我一直在寻找的快乐代码!

Thank you to Dave R, Earwicker, and TheSoftwareJedi for sending me down the Linq path. It is just the sort of happy code I was looking for!

推荐答案

看起来您正在尝试笛卡尔式连接两个列表,并应用where子句.这是一个简单的示例,显示了执行此操作的Linq语法,我想这就是您要寻找的. list1和list2可以是任何IEnumerable,您的where子句可以包含更详细的逻辑,在select子句中,您可以提取所需的内容.

Looks like you are trying to cartesian join two lists, and apply a where clause. Here's a simple example showing the Linq syntax for doing this, which I think is what you are looking for. list1 and list2 can be any IEnumerable, your where clause can contain more detailed logic, and in your select clause you can yank out what you need.

        var list1 = Enumerable.Range(1, 100);
        var list2 = Enumerable.Range(1, 100);

        foreach (var item in from a in list1
                             from b in list2
                             where a % b == 0
                             select new { a, b })
        {
            Console.WriteLine(item);
        };

性能将与您发布的内容相同-不想在这方面误导.我确实更喜欢这种Linq语法.

Performance will be identical to what you posted though - no desire to mislead on that respect. I do prefer this Linq syntax.

这篇关于称呼其他代码书呆子-嵌套循环的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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