比较C#中的2个列表 [英] Comparing 2 Lists in C#

查看:66
本文介绍了比较C#中的2个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,我是编码的新手,这是我的第一个正式申请.

我需要为客户编写一个应用程序,在其中将数据输入到文本"框中.

然后需要在后台比较数据,并在下面显示结果.

例子
IE.方框A包含:1 2 3 4
方框B包含:2 3 4 5

结果应为:
方框A:5
方框B:1

此应用程序的原因是用户将从两个来源粘贴订单号列表.将比较这两个列表并显示结果.这意味着需要输入和分离粘贴的数据(以便每个订单号在列表中都有它自己的值).

我已经显示了正在工作的数据.而且我认为数据拆分是有效的.我现在需要做的是比较数据,并确认将数据拆分为单独的条目.

这是我目前正在使用的代码:

 // 左侧收藏条目
           List< string> leftCollection =  List< string>();
           leftCollection.Add(txtLeftCollection.Text);

           txtLeftResult.Text = Convert.ToString(leftCollection);
           txtLeftResult.Text = " ;
           txtLeftResult.Text + = leftCollection [ 0 ];

           字符串 splitLeft = Convert.ToString(leftCollection);

           字符串 [] wordsL = splitLeft.Split('  \ n ');
            foreach (字符串单词 in  wordsL)
           {
               Console.WriteLine(word);
           }

           // 权利收集条目
           List< string> rightCollection =  List< string>();
           rightCollection.Add(txtRightCollection.Text);

           txtRightResult.Text = Convert.ToString(rightCollection);
           txtRightResult.Text = " ;
           txtRightResult.Text + = rightCollection [ 0 ];


           字符串 splitRight = Convert.ToString(leftCollection);

           字符串 [] wordsR = splitRight.Split('  \ n ');
            foreach (字符串单词 in  wordsR)
           {
               Console.WriteLine(word);
           }



有人可以帮我吗.

提前谢谢您.

Corne

解决方案

检查本文

http://www.c-sharpcorner .com/UploadFile/1a81c5/custom-extension-method-to-compare-list-in-C-Sharp/ [ 命名空间 ConsoleApplication17 { class 程序 { 静态 无效 Main() { 字符串 s1 = " ; 字符串 s2 = " ; 字符串 [] arrayOne = s1.Split(' '); 字符串 [] arrayTwo = s2.Split(' '); 字符串左= 字符串 .Empty; 字符串 Right = 字符串 .Empty; foreach (字符串单词 in arrayOne中) { 如果(!(arrayTwo.Contains(word))) { 如果(左== string .Empty) 左=字; 其他 左=左+ " +单词; } } foreach (字符串单词 in arrayTwo中) { 如果(!(arrayOne.Contains(word))) { 如果(正确== string .Empty) 右=字; 其他 右=右+ " +单词; } } Console.WriteLine(左); Console.WriteLine(右); Console.Read(); } } }


感谢您的帮助

这是我的解决方案

 {
            // 清除以前的结果
            txtLeftResult.Text = " ;
            txtRightResult.Text = " ;

            // 左侧收藏条目
            List< string> leftCollection =  List< string>();
            leftCollection.AddRange(txtLeftCollection.Text.Split( 字符串 [] {  \ r \ n"},StringSplitOptions.None)));
            // 权利收集条目
            List< string> rightCollection =  List< string>();
            rightCollection.AddRange(txtRightCollection.Text.Split( 字符串 [] {  \ r \ n"},StringSplitOptions.None)));

            // 在左侧集合中循环
             foreach (字符串 in  leftCollection中的行)
            {
                如果(!rightCollection.Contains(row))
                {
                    txtLeftResult.Text = txtLeftResult.Text + "  +行;
                }
            }
            如果(txtLeftResult.Text.Length >   0 )
            {
               txtLeftResult.Text = txtLeftResult.Text.Remove( 0  2 );
            }

            // 通过权利收集实现循环
             foreach (字符串 rightCollection中的行)
            {
                如果(!leftCollection.Contains(row))
                {
                    txtRightResult.Text = txtRightResult.Text + "  +行;
                }
            }
            如果(txtRightResult.Text.Length >   0 )
            {
                txtRightResult.Text = txtRightResult.Text.Remove( 0  2 );
            }


            
        } </ 字符串 >  </ 字符串 >  </ 字符串 >  字符串 //Left Collection Entries
           List<string> leftCollection = new List<string>();
           leftCollection.Add(txtLeftCollection.Text);

           txtLeftResult.Text = Convert.ToString(leftCollection);
           txtLeftResult.Text = "";
           txtLeftResult.Text += leftCollection[0];

           string splitLeft = Convert.ToString(leftCollection);

           string[] wordsL = splitLeft.Split('\n');
           foreach (string word in wordsL)
           {
               Console.WriteLine(word);
           }

           //Right Collection Entries
           List<string> rightCollection = new List<string>();
           rightCollection.Add(txtRightCollection.Text);

           txtRightResult.Text = Convert.ToString(rightCollection);
           txtRightResult.Text = "";
           txtRightResult.Text += rightCollection[0];


           string splitRight = Convert.ToString(leftCollection);

           string[] wordsR = splitRight.Split('\n');
           foreach (string word in wordsR)
           {
               Console.WriteLine(word);
           }



Can someone please help me on this.

Thank you in advance.

Corne

解决方案

Check this article

http://www.c-sharpcorner.com/UploadFile/1a81c5/custom-extension-method-to-compare-list-in-C-Sharp/[^]


Try this

namespace ConsoleApplication17
{
    class Program
    {
        static void Main()
        {
            string s1 = "1 2 3 4";
            string s2 = "2 3 4 5";



            string[] arrayOne = s1.Split(' ');
            string[] arrayTwo= s2.Split(' ');
            string Left=string.Empty;
            string Right=string.Empty;
            foreach (string word in arrayOne)
            {
                if (!(arrayTwo.Contains(word)))
                {
                    if (Left == string.Empty)
                        Left = word;
                    else
                        Left = Left + " " + word;
                }
            }
            foreach (string word in arrayTwo)
            {
                if (!(arrayOne.Contains(word)))
                {
                    if (Right == string.Empty)
                        Right = word;
                    else
                        Right = Right + " " + word;
                }
            }

            Console.WriteLine(Left);
            Console.WriteLine(Right);
            Console.Read();
        }

    }
}


Thank you for the help

here is my solution

{
            //Clear previous Results
            txtLeftResult.Text = "";
            txtRightResult.Text = "";

            //Left Collection Entries
            List<string> leftCollection = new List<string>();
            leftCollection.AddRange(txtLeftCollection.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None));            
            //Right Collection Entries
            List<string> rightCollection = new List<string>();
            rightCollection.AddRange(txtRightCollection.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None));

            //Loop through left collection
            foreach (string row in leftCollection)
            {
                if (!rightCollection.Contains(row))
                {
                    txtLeftResult.Text = txtLeftResult.Text + "\r\n" + row;
                }
            }
            if (txtLeftResult.Text.Length > 0)
            {
               txtLeftResult.Text = txtLeftResult.Text.Remove(0, 2);
            }

            //Loop through Right collection
            foreach (string row in rightCollection)
            {
                if (!leftCollection.Contains(row))
                {
                    txtRightResult.Text = txtRightResult.Text + "\r\n" + row;
                }
            }
            if (txtRightResult.Text.Length > 0)
            {
                txtRightResult.Text = txtRightResult.Text.Remove(0, 2);
            }


            
        }</string></string></string></string>


这篇关于比较C#中的2个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆