簡體   English   中英

搜索等於寬度LINQ

[英]search equals width LINQ

我有一個通用的列表列表,試圖確定每個列表中是否已經有五個相等的數字。 如果在列表中找不到equals,那么將list添加到列表中這段代碼可以工作,但我想了解更多關於linq的信息。

如何使用LINQ做到這一點。 謝謝

private void button2_Click(object sender, EventArgs e)
    {
        int n1 = (int)numericUpDown1.Value;
        int n2 = (int)numericUpDown2.Value;
        int n3 = (int)numericUpDown3.Value;
        int n4 = (int)numericUpDown4.Value;
        int n5 = (int)numericUpDown5.Value;
        int n6 = (int)numericUpDown6.Value;
        int n7 = (int)numericUpDown7.Value;
        int n8 = (int)numericUpDown8.Value;
        int n9 = (int)numericUpDown9.Value;
        int n10 = (int)numericUpDown10.Value;
        int n11 = (int)numericUpDown11.Value;
        int n12 = (int)numericUpDown12.Value;
            list = new List<int>();
            list.Add(n1);
            list.Add(n2);
            list.Add(n3);
            list.Add(n4);
            list.Add(n5);
            list.Add(n6);
            list.Add(n7);
            list.Add(n8);
            list.Add(n9);
            list.Add(n10);
            list.Add(n11);
            list.Add(n12);
            if (data.Count == 0)
                data.Add(list);
            else
            {
                int l = data.Count;
                bool eq =false;
                for (int i = 0; i < l; i++)
                {
                    int count = 0;
                    foreach (int n in list)
                    {
                        if (data[i].IndexOf(n) != -1)
                            ++count;
                        if (count == 5)
                        {
                            eq = true;
                            break;
                        }
                    }
                    if (eq == true)
                        break;
                }
                if (eq == false)
                    data.Add(list);
                else
                {
                    // do nothing
                }
            }

    }

試圖確定每個列表中是否已有五個相等的數字。 如果沒有,則將它們添加到列表中

您可以組合Enumerable.Count和循環,例如:

int n1 = (int)numericUpDown1.Value;
foreach(List<int> list in data)
{
    int count = list.Count(i => i == n1);
    while(count++ < 5)
        list.Add(n1);
}

您可以使用IntersectCount擴展方法。

就像是

var exist = false;
foreach (var existingList in data) {
  if (existingList.Intersect(list).Count() >=5) {
    exist = true;
    break;
}

if (!exist) data.Add(list);

但是根據列表的大小,這將大大降低性能,因為“檢查交集> = 5”將與列表的所有數據相交。

[編輯] - 請參閱下面的[更新]

我相信您當前的代碼應如下所示:

...
int count = 0;
for (int i = 0; i < l; i++)
{
    //int count = 0;
    foreach (int n in list)
    {
...

無論如何,要回答你的問題(如果我理解你想要實現的目標),你可以使用:

class Program
{
    static List<List<int>> data;
    static List<int> list;
    static void Main(string[] args)
    {
        data = new List<List<int>>();

        for (int i = 0; i < 6; i++)
        {
            list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(1);

            var result = data
                .Union(new[]{list})
                .SelectMany(j => j)
                .GroupBy(j => j)
                .Select(j => new { j.Key, j })
                .Where(j => j.j.Count() > 4);

            if (result.Count() == 0)
                data.Add(list);

        }
    }
}

[UPDATE]

好吧,我想我明白了你想要實現的目標:如果data中沒有其他列表與list至少有5個共同的list ,那么應該將list添加到data ,這是一個List<List<int>>

var result = data.Any(i => i.Intersect(list).Count() > 4);
if(!result)
    data.Add(list);

鑒於您發布的代碼,我認為解決方案是:

List<int> list = new List<int>();
List<List<int>> data = new List<List<int>>();

if (data.All(l => l.Intersect(list).Count() < 5))
    data.Add(list);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM