简体   繁体   中英

search equals width LINQ

I have a generic list of lists, trying to determine if there are already five equal numbers in each list. If not find equals in lists, then add list into lists this code work but I like learn more about linq.

how can do this using LINQ. thank you

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
                }
            }

    }

trying to determine if there are already five equal numbers in each list. If not, then add them into lists

You can combine Enumerable.Count and loops, for example:

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

you could use Intersect and Count extension methods.

something like

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

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

But depending on the size of your lists, this will be far less performant, as the "check for intersects >= 5" will intersect all data of the lists.

[EDIT] - please see the [UPDATE] below

I believe that your current code should look like:

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

Anyway, to answer your question (if I understood correctly what you want to achieve) you may use this:

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]

Ok, I think I understood what you want to achieve: if there are no other lists in data that have at least 5 elements in common with the list , the list should be added to the data , which is a List<List<int>> .

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

Given your code you've posted, I think the solution is:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM