Re-use List again

MiPakTeh 1,476 Reputation points
2022-12-18T09:52:13.81+00:00

Hi All,
I have count number after find 3 digits in Button_1 and then put to the list D4.
My question is how to use back D4 in Button_2.

My code test;

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Checking_13  
{  
    public partial class Form1 : Form  
    {  
        public static List<string> D1 = new List<string>();  
        public static List<string> D2 = new List<string>();  
  
        public static List<string> D3 = new List<string>();  
        public static List<string> D4 = new List<string>();  
        public static List<string> D5 = new List<string>();  
        public static List<int> D6 = new List<int>();  
  
  
         List<int> interested = new List<int>();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
        private void Form1_Load(object sender, EventArgs e)  
        {  
  
            string centerIds_list = ("101, 221, 324, 471, 114, 233, 341, 443, 101, 324, 728");  
            List<string> result = centerIds_list?.ToString().Split(',').Select(s => s.Trim()).ToList();  
            foreach (var Val in result)  
            {  
                listBox1.Items.Add(Val);  
                D1.Add(Val);  
            }  
  
            string centerIds_list_ = ("742, 101, 728, 221, 384, 471, 114, 233, 349, 443, 101, 247");  
            List<string> result_ = centerIds_list_?.ToString().Split(',').Select(s => s.Trim()).ToList();  
  
            foreach (var Val_ in result_)  
            {  
                listBox2.Items.Add(Val_);  
                D2.Add(Val_);  
            }  
  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            var c = D2.Where(x => D1.Contains(x));  
            foreach (var voc in c)  
            {  
                D3.Add(voc);  
            }  
  
            var r =D3.Cast<string>().Select(i => i.Split(',').Select(n => string.Concat(n.Trim().OrderBy(f => f))))  
                .SelectMany(a => a)  
                .GroupBy(n => n)  
                .Select(g => new { n = g.Key, f = g.Count() });  
  
            string[] LCount = r.Select(z => $"{z.n} = {z.f}").ToArray();  
            listBox3.Items.AddRange(LCount);  
  
  
            D4.Add(LCount.ToString());  
  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
  
            var SameNum = D4.Select(n => n.ToString()).Where(n => n.Distinct().Count() == 3).ToArray();  
  
            foreach (var Num_ in SameNum)  
            {  
                listBox4.Items.Add(Num_);  
            }  
  
        }  
  
        private void button3_Click(object sender, EventArgs e)  
        {  
  
        }  
  
        private void button4_Click(object sender, EventArgs e)  
        {  
  
        }  
  
        private void button5_Click(object sender, EventArgs e)  
        {  
  
        }  
    }  
}  
  
  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-12-19T07:05:32.31+00:00

    Hi @MiPakTeh , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    //D4.Add(LCount.ToString());  
    foreach (var item in LCount)  
    {  
        D4.Add(item);  
    }  
    

    And:

    var SameNum = D4.Select(n => n).Where(n => n.Distinct().Count() == 5).ToArray();  
    

    271983-image.png

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.