Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用一个performant LINQ查询将大整数数组拆分为多个整数组_C#_.net_Linq - Fatal编程技术网

C# 使用一个performant LINQ查询将大整数数组拆分为多个整数组

C# 使用一个performant LINQ查询将大整数数组拆分为多个整数组,c#,.net,linq,C#,.net,Linq,我想把一个大整数数组分为两组,一组是4的倍数,另一组是5的倍数。如何仅使用一个查询就可以做到这一点?注意性能,这对我来说非常重要 为了进一步解释我需要什么,假设我的数字列表是{2,7,8,10,12,14,19,20,25},那么我希望我的输出是: new[] { new { Remainder = 4, Numbers = new List<int>(){ 8, 12, 20} }, new {

我想把一个大整数数组分为两组,一组是4的倍数,另一组是5的倍数。如何仅使用一个查询就可以做到这一点?注意性能,这对我来说非常重要

为了进一步解释我需要什么,假设我的数字列表是{2,7,8,10,12,14,19,20,25},那么我希望我的输出是:

new[]
{
    new
    {
        Remainder = 4,
        Numbers = new List<int>(){ 8, 12, 20}
    },
    new
    {
        Remainder = 5,
        Numbers = new List<int>(){10, 20, 25}
    }
}
正如你所看到的,它让我接近了两个查询,但正如我所说,我想要一个查询。

你可以使用:

简单地连接两个序列

现在还不完全清楚为什么要使用GroupBy,然后筛选Key==0。余数将始终为0

也许一个简单的地方就足够了? 您可以使用逻辑OR | |组合查询:

回应你的评论:你在找这样的东西吗

var result = new[] {4, 5}
             .Select(d => new 
                    { 
                        Divider = d, 
                        Values = numbers.Where(n => n % d == 0).ToList()
                    });

有两种LINQ方法可用于此:

//This will join the lists, excluding values that already appear once
var result = numberGroupsTimes5.Union(numberGroupsTimes4)

//This will simply append one list the the other
var result = numberGroupsTimes5.Concat(numberGroupsTimes4)
你是说

var numberGroupsTimes4or5 = from n in numbers
                            group n by n into g
                            where g.Key % 4 == 0 || g.Key % 5 == 0
                            select new { Remainder = g.Key, Numbers = g };
也许是这个

var result = new[] { 4, 5 }
            .SelectMany(x => numbers.Select(n => (n, x)))
            .Where(g => g.n % g.x == 0)
            .GroupBy(g => g.x, (Key, g) => 
                    new { Remainder = Key, Numbers = g.Select(z => z.n) });
这就产生了这个结果

这里有一个类似的方法,但这次使用的查询语法与您的问题类似

var numbersAndRemainders = new[] { 4, 5 }
            .SelectMany(rem => numbers.Select(n => (n, rem)));

        var numberGroups =
            from n in numbersAndRemainders
            group n by new { remainder = n.n % n.rem, n.rem } into g
            where g.Key.remainder == 0
            select new { Remainder = g.Key.rem, Numbers = g.Select(z => z.n) };
var numberGroupsTimes4or5 = from n in numbers
                            group n by n into g
                            where g.Key % 4 == 0 || g.Key % 5 == 0
                            select new { Remainder = g.Key, Numbers = g };
var result = new[] { 4, 5 }
            .SelectMany(x => numbers.Select(n => (n, x)))
            .Where(g => g.n % g.x == 0)
            .GroupBy(g => g.x, (Key, g) => 
                    new { Remainder = Key, Numbers = g.Select(z => z.n) });
var numbersAndRemainders = new[] { 4, 5 }
            .SelectMany(rem => numbers.Select(n => (n, rem)));

        var numberGroups =
            from n in numbersAndRemainders
            group n by new { remainder = n.n % n.rem, n.rem } into g
            where g.Key.remainder == 0
            select new { Remainder = g.Key.rem, Numbers = g.Select(z => z.n) };