Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# 从c中的主列表创建多个唯一条目列表#_C# - Fatal编程技术网

C# 从c中的主列表创建多个唯一条目列表#

C# 从c中的主列表创建多个唯一条目列表#,c#,C#,我需要处理一个出站SMS队列并创建一批消息。排队列表可能包含发送给同一个人的多条消息。批处理不允许这样做,因此我需要运行主出站队列并创建尽可能多的批处理,以确保它们包含唯一的条目。 例如: 结果 batch 1 = (1,2,3,4,5,6,7,8,9) batch 2 = (3,7,8) batch 3 = (7,8) batch 4 = (8) 我可以很容易地检查重复,但我正在寻找一种巧妙的方法来生成额外的批次 谢谢 根据使用的特定数据结构,可以使用Linq扩展方法(前提

我需要处理一个出站SMS队列并创建一批消息。排队列表可能包含发送给同一个人的多条消息。批处理不允许这样做,因此我需要运行主出站队列并创建尽可能多的批处理,以确保它们包含唯一的条目。 例如:

结果

 batch 1 = (1,2,3,4,5,6,7,8,9)
    batch 2 = (3,7,8)
    batch 3 = (7,8)
batch 4 = (8)
我可以很容易地检查重复,但我正在寻找一种巧妙的方法来生成额外的批次


谢谢

根据使用的特定数据结构,可以使用Linq扩展方法(前提是队列对某些类型的
T
实现
IEnumerable
)由同一用户进行分组;然后,可以分别迭代这些组。

使用
Enumerable.ToLookup
和其他LINQ方法来了解这种方法:

var queues = new int[] { 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9 };
var lookup = queues.ToLookup(i => i);
int maxCount = lookup.Max(g => g.Count());
List<List<int>> allbatches = Enumerable.Range(1, maxCount)
    .Select(count => lookup.Where(x => x.Count() >= count).Select(x => x.Key).ToList())
    .ToList();
var queues=newint[]{1,2,3,3,4,5,6,7,7,8,8,8,9};
var lookup=queues.ToLookup(i=>i);
int maxCount=lookup.Max(g=>g.Count());
List allbatches=可枚举范围(1,最大计数)
.Select(count=>lookup.Where(x=>x.count()>=count)。Select(x=>x.Key)。ToList())
.ToList();
结果是一个包含其他四个
列表的列表:

foreach(所有批次中的列表)
Console.WriteLine(string.Join(“,”,list));
1, 2, 3, 4, 5, 6, 7, 8, 9
3, 7, 8
8.
8.

一种简单的方法是遍历输入,边走边创建和填充批次:

private static List<List<int>> CreateUniqueBatches(List<int> source)
{
    var batches = new List<List<int>>();

    int currentBatch = 0;

    foreach (var i in source)
    {
        // Find the index for the batch that can contain the number `i`
        while (currentBatch < batches.Count && batches[currentBatch].Contains(i))
        {
            currentBatch++;
        }

        if (currentBatch == batches.Count)
        {
            batches.Add(new List<int>());
        }

        batches[currentBatch].Add(i);
        currentBatch = 0;
    }

    return batches;
}

我相信这可以缩短或写在一个功能的方式。我试过使用GroupBy、Distinct和Except,但没能那么快找到答案

你说的是批处理和消息,这些是集合吗?如果是,哪种类型的集合(例如
int[]
列表
)?如果队列对您的问题不重要,那么您使用的术语(如队列)只是令人困惑。如果可能,请提供显示您正在尝试执行的操作的编译代码。到目前为止,您尝试了哪些操作以及您遇到了哪些困难?这似乎很容易做到。显示你的代码,这样你就可以得到一个具体的答案。我仍然不确定我是否理解正确。为什么你们有4批而不是3批?第一个包含所有不同的数字,第二个包含至少出现两次的所有数字,第三个包含至少出现三次的所有数字,但只有8。第3批中有什么?@Tim OP希望将所有输入划分到各个批中,以便每个批包含不同的值。OP对第3批的结果是错误的,它应该只包含一个
8
,因为在第1批和第2批的输入中只有2个
7
s。但是在输入中有4个
8
s。每批应该包含一个
8
,并且应该有4批。@CodeCaster:最后一分钱掉了!编辑我的答案以提供仅限LINQ的解决方案。
foreach (List<int> list in allbatches)
    Console.WriteLine(string.Join(",", list));

1, 2, 3, 4, 5, 6, 7, 8, 9
3, 7, 8
8
8
private static List<List<int>> CreateUniqueBatches(List<int> source)
{
    var batches = new List<List<int>>();

    int currentBatch = 0;

    foreach (var i in source)
    {
        // Find the index for the batch that can contain the number `i`
        while (currentBatch < batches.Count && batches[currentBatch].Contains(i))
        {
            currentBatch++;
        }

        if (currentBatch == batches.Count)
        {
            batches.Add(new List<int>());
        }

        batches[currentBatch].Add(i);
        currentBatch = 0;
    }

    return batches;
}
1, 2, 3, 4, 5, 6, 7, 8, 9
3, 7, 8
8
8