Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_Linq - Fatal编程技术网

C# 如何在字符串数组中分别获取不同的值?

C# 如何在字符串数组中分别获取不同的值?,c#,linq,C#,Linq,我有两个字符串数组 string[] oldname = ["arun","jack","tom"]; string[] newname = ["jack","hardy","arun"]; 在这里,我想比较这两个字符串数组以分别获得这些不同的值,如下所示: oldname = ["tom"]; newname = ["hardy"]; 如何实现这些…让这两个数组定义如下: string[] oldname = new[] { "arun", "jack", "tom" }; strin

我有两个字符串数组

string[] oldname = ["arun","jack","tom"];
string[] newname = ["jack","hardy","arun"];
在这里,我想比较这两个字符串数组以分别获得这些不同的值,如下所示:

oldname = ["tom"];
newname = ["hardy"];

如何实现这些…

让这两个数组定义如下:

 string[] oldname = new[] { "arun", "jack", "tom" };
 string[] newname = new string[] { "jack", "hardy", "arun" };
然后,您可以使用扩展方法
。除了
之外,以实现您要查找的结果。考虑下面的代码和


假设两个数组的定义如下所示:

 string[] oldname = new[] { "arun", "jack", "tom" };
 string[] newname = new string[] { "jack", "hardy", "arun" };
然后,您可以使用扩展方法
。除了
之外,以实现您要查找的结果。考虑下面的代码和

试试这个:

    string[] oldname = new string[] { "arun", "jack", "tom" };
    string[] newname = new string[] { "jack", "hardy", "arun" };

    List<string> distinctoldname = new List<string>();
    List<string> distinctnewname = new List<string>();

    foreach (string txt in oldname)
    {
       if (Array.IndexOf(newname, txt) == -1)
         distinctoldname.Add(txt);
     }

     foreach (string txt in newname)
     {
        if (Array.IndexOf(oldname, txt) == -1)
          distinctnewname.Add(txt);
     }


   //here you can get both the arrays separately
string[]oldname=新字符串[]{“arun”、“jack”、“tom”};
string[]newname=新字符串[]{“jack”、“hardy”、“arun”};
List distinctoldname=新列表();
List distinctnewname=新列表();
foreach(旧名称中的字符串txt)
{
if(Array.IndexOf(newname,txt)=-1)
distinctoldname.Add(txt);
}
foreach(新名称中的字符串txt)
{
if(Array.IndexOf(oldname,txt)=-1)
distinctnewname.Add(txt);
}
//在这里,您可以分别获得这两个阵列
希望得到以下帮助:)

尝试以下方法:

    string[] oldname = new string[] { "arun", "jack", "tom" };
    string[] newname = new string[] { "jack", "hardy", "arun" };

    List<string> distinctoldname = new List<string>();
    List<string> distinctnewname = new List<string>();

    foreach (string txt in oldname)
    {
       if (Array.IndexOf(newname, txt) == -1)
         distinctoldname.Add(txt);
     }

     foreach (string txt in newname)
     {
        if (Array.IndexOf(oldname, txt) == -1)
          distinctnewname.Add(txt);
     }


   //here you can get both the arrays separately
string[]oldname=新字符串[]{“arun”、“jack”、“tom”};
string[]newname=新字符串[]{“jack”、“hardy”、“arun”};
List distinctoldname=新列表();
List distinctnewname=新列表();
foreach(旧名称中的字符串txt)
{
if(Array.IndexOf(newname,txt)=-1)
distinctoldname.Add(txt);
}
foreach(新名称中的字符串txt)
{
if(Array.IndexOf(oldname,txt)=-1)
distinctnewname.Add(txt);
}
//在这里,您可以分别获得这两个阵列

希望有帮助:)

您必须使用阵列吗?你们能使用列表吗?两个数组的大小是否相同?但你们的结果并没有显示“不同”的值。。。如果您要求distinct,olname和newname必须按原样打印,因为它们是“distinct”的。您必须使用数组吗?你们能使用列表吗?两个数组的大小是否相同?但你们的结果并没有显示“不同”的值。。。如果你问清楚,olname和newname必须按原样打印,因为它们是“不同的”,这是可行的,但我相信它留下了一个空白。如果有倍数呢?@MichaelBedford:对不起,我不明白你的意思,输入会有什么变化?你能试一下我在答案中添加的工作示例吗?这是可行的,但我相信它留下了一个空白。如果有倍数呢?@MichaelBedford:对不起,我不明白你的意思,输入会有什么变化?你能试一下我在答案中添加的工作示例吗?
string[] oldname = new []{"arun","jack","tom"};
string[] newname = new []{"jack","hardy","arun"};

// use linq to loop through through each list and return values not included in the other list.
var distinctOldName = oldname.Where(o => newname.All(n => n != o));
var distinctNewName = newname.Where(n => oldname.All(o => o != n));

distinctOldName.Dump(); // result is tom
distinctNewName.Dump(); // result is hardy