生成 .NET 中多个列的所有可能组合

发布于 2024-09-15 19:55:24 字数 173 浏览 2 评论 0原文

我的文件中有 4 列,每列或多或少包含多个值。我需要获得所有可能的组合,而结果字符串中的列顺序和部分数量应保持不变。

例如,第一列包含范围为 1-100 的数字,第二列:字母 az,第三列也是数字,我会得到类似

1-A-1、2-A-1、3-A-1 的内容; 1-B-1、1-B-2、1-B-3 等等。

I have like 4 columns in a file, each column contains a number of values, more or less. I need to get all possible combinations, while order of columns and number of sections in resulting strings should remain the same.

E.g. first column contains numbers ranging 1-100, second: letters a-z, and the third one is also numeric, I would get something like

1-A-1, 2-A-1, 3-A-1; 1-B-1, 1-B-2, 1-B-3 and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-09-22 19:55:24

有多种通用方法可以实现此目的,但对于您确切知道有多少列的情况,最简单的方法就是使用嵌套循环:

foreach (var a in firstColumn)
{
    foreach (var b in secondColumn)
    {
        foreach (var c in thirdColumn)
        {
           foreach (var d in fourthColumn)
           {
               // Do something with a, b, c, d
           }
        }
    }
}

使用 LINQ 的替代方法:

var query = from a in firstColumn
            from b in secondColumn
            from c in thirdColumn
            from d in fourthColumn
            select new { a, b, c, d };
foreach (var tuple in query)
{
    // Do something with tuple.a, tuple.b etc
}

There are various general-purpose approaches to this, but the simplest for a case where you know exactly how many columns you've got is just to use nested loops:

foreach (var a in firstColumn)
{
    foreach (var b in secondColumn)
    {
        foreach (var c in thirdColumn)
        {
           foreach (var d in fourthColumn)
           {
               // Do something with a, b, c, d
           }
        }
    }
}

Alternative using LINQ:

var query = from a in firstColumn
            from b in secondColumn
            from c in thirdColumn
            from d in fourthColumn
            select new { a, b, c, d };
foreach (var tuple in query)
{
    // Do something with tuple.a, tuple.b etc
}
把梦留给海 2024-09-22 19:55:24

假设您已将文件解析为 Foo 对象列表,其中每个属性都包含一列的值:

class Foo
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
}

List<Foo> list = ParseFile();


var query = from f1 in list
            from f2 in list
            from f3 in list
            from f4 in list
            select new Foo
            {
                Column1 = f1.Column1,
                Column2 = f2.Column2,
                Column3 = f3.Column3,
                Column4 = f4.Column4,
            };

Foo[] combinations = query.ToArray();

Assuming you have parsed the file into a list of Foo objects where each property contains the value of a column:

class Foo
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
}

List<Foo> list = ParseFile();


var query = from f1 in list
            from f2 in list
            from f3 in list
            from f4 in list
            select new Foo
            {
                Column1 = f1.Column1,
                Column2 = f2.Column2,
                Column3 = f3.Column3,
                Column4 = f4.Column4,
            };

Foo[] combinations = query.ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文