Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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将2个字符串数组组合成一个具有唯一值的数组_C#_Arrays_Linq_Unions - Fatal编程技术网

C# C将2个字符串数组组合成一个具有唯一值的数组

C# C将2个字符串数组组合成一个具有唯一值的数组,c#,arrays,linq,unions,C#,Arrays,Linq,Unions,这与a有关,但似乎不同,可以单独发布: 我的设置存储在用户输入domAttributeId和IntlatTributeId的字符串数组中。我试图有条件地构建一个值数组,以便在其上执行一些代码。条件是: 如果存在DomAttributeSettings,则创建一个值数组并将其传递给AddAttributeEdit方法。 如果存在IntlatAttributeSettings,请创建这些设置的数组,并将它们与condition one中的设置组合起来,然后将该数组传递给代码的其余部分,最后将每个数组

这与a有关,但似乎不同,可以单独发布:

我的设置存储在用户输入domAttributeId和IntlatTributeId的字符串数组中。我试图有条件地构建一个值数组,以便在其上执行一些代码。条件是:

如果存在DomAttributeSettings,则创建一个值数组并将其传递给AddAttributeEdit方法。 如果存在IntlatAttributeSettings,请创建这些设置的数组,并将它们与condition one中的设置组合起来,然后将该数组传递给代码的其余部分,最后将每个数组元素传递给AddAttributeEdit方法。 除了无重复部分之外,下面的代码似乎可以工作。我认为使用Linq的联合和/或独特的方法可以做到这一点,但我肯定做错了什么。我的代码引发以下异常:

发现多个具有相同ID“trAttribute_493”的控件。FindControl要求控件具有唯一的ID

我知道这是因为这两种设置中都存在该id。最后的AddAttributeEdit方法包含基于传递给它的值构建具有ID的表和单元格的代码。为了得到两个数组的一个明显的并集,我做错了什么

代码如下:

    private void LoadAttributes()
    {
        if (!string.IsNullOrEmpty(DomAttributesSetting))
        {
            string[] attributeIds;
            if (!string.IsNullOrEmpty(IntlAttributesSetting))
            {

                string[] domattributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] intlattributeIds = IntlAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                IEnumerable<string> atrributeIdList = domattributeIds.Union(intlattributeIds).Distinct();
                attributeIds = atrributeIdList.ToArray();
            }
            else
            {
                attributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            } 
            foreach (string attributeId in attributeIds)
            {
                int attrId = -1;
                if (int.TryParse(attributeId, out attrId) && attrId != -1)
                {
                    Arena.Core.Attribute attribute = new Arena.Core.Attribute(attrId);
                    PersonAttribute personAttribute = (PersonAttribute)person.Attributes.FindByID(attribute.AttributeId);
                    if (personAttribute == null)
                        personAttribute = new PersonAttribute(person.PersonID, attrId);

                    AddAttributeEdit(attribute, personAttribute, true);
                }

            }


        }

    }...

使用LINQ中的Union,例如:

string[] domattributeIds = new string[] { "a", "b", "c" };
string[] intlattributeIds = new string[] { "a", "c", "d" };

string[] combined = domattributeIds.Union(intlattributeIds).ToArray();

组合的新数组具有值a、b、c、d,使用LINQ中的Union,例如:

string[] domattributeIds = new string[] { "a", "b", "c" };
string[] intlattributeIds = new string[] { "a", "c", "d" };

string[] combined = domattributeIds.Union(intlattributeIds).ToArray();

新组合的数组具有值a、b、c、d

您可能在逗号分隔列表中的值周围有填充,例如Prop1、Prop2与Prop1、Prop2,或者大小写不同

尝试从输入中修剪每个值,并使用不区分大小写的比较器,如下所示:

var domattributeIds = DomAttributesSetting
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(x=>x.Trim());

var intlattributeIds = IntlAttributesSetting
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(x=>x.Trim());

var attributeIds = domattributeIds
    .Union(intlattributeIds, StringComparer.InvariantCultureIgnoreCase)
    .ToArray();

您甚至不需要在末尾调用Distinct。

在逗号分隔列表中的值周围可能有填充,例如Prop1、Prop2与Prop1、Prop2或大小写不同

 string a = "a,b, c";
 string b = "b,c,d";
 String[] ass  = a.Split( new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
 String[] bss =  b.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
 var list = ass.Union(bss).Distinct().ToArray();
 StringBuilder sb = new StringBuilder();
 foreach ( String s in list)
  {
    sb.Append(s);
  }
 Text = sb.ToString();
尝试从输入中修剪每个值,并使用不区分大小写的比较器,如下所示:

var domattributeIds = DomAttributesSetting
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(x=>x.Trim());

var intlattributeIds = IntlAttributesSetting
    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(x=>x.Trim());

var attributeIds = domattributeIds
    .Union(intlattributeIds, StringComparer.InvariantCultureIgnoreCase)
    .ToArray();
你甚至不需要在最后调用Distinct

 string a = "a,b, c";
 string b = "b,c,d";
 String[] ass  = a.Split( new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
 String[] bss =  b.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
 var list = ass.Union(bss).Distinct().ToArray();
 StringBuilder sb = new StringBuilder();
 foreach ( String s in list)
  {
    sb.Append(s);
  }
 Text = sb.ToString();
由于篇幅的原因,最后的文本是ab ccd。确保您的输入不包含空格


由于篇幅的原因,最后的文本是ab ccd。确保您的输入不包含空格

某些整数必须具有不同的字符串表示形式,例如前导0

最好先转换为整数,然后运行Distinct

比如:

var ints = attributeIds.Select( s =>
{
  int i;
  return Int32.TryParse( s, out i ) ? i : -1;
};

var uniques = ints.Where( i => i != -1 ).Distinct().ToList();

foreach ( int attrId in uniques )
{
  Arena.Core.Attribute attribute = ...

某些整数必须具有不同的字符串表示形式,例如前导0

最好先转换为整数,然后运行Distinct

比如:

var ints = attributeIds.Select( s =>
{
  int i;
  return Int32.TryParse( s, out i ) ? i : -1;
};

var uniques = ints.Where( i => i != -1 ).Distinct().ToList();

foreach ( int attrId in uniques )
{
  Arena.Core.Attribute attribute = ...

您是否有两个字符串仅因大小写而异?默认情况下,Linq不会将这些视为重复项。请尝试以下方法:

domattributeIds.Union(intlattributeIds, StringComparer.CurrentCultureIgnoreCase)

您是否有两个字符串仅因大小写而异?默认情况下,Linq不会将这些视为重复项。请尝试以下方法:

domattributeIds.Union(intlattributeIds, StringComparer.CurrentCultureIgnoreCase)

下面的代码将从2个字符串数组返回不同的字符串数组

public string[] UniqueNames(string[] names1, string[] names2) {
    newName = names1.ToList();
    foreach (var dr in names2) {
        if (!newName.Contains(dr)) {
            newName.Add(dr);
        }
    }
    string[] newNameArray = newName.ToArray();
    return newNameArray;
}
或者你也可以试试

public string[] UniqueNames(string[] names1, string[] names2) {            
    return names1.Union(names2).ToArray(); ;            
}

下面的代码将从2个字符串数组返回不同的字符串数组

public string[] UniqueNames(string[] names1, string[] names2) {
    newName = names1.ToList();
    foreach (var dr in names2) {
        if (!newName.Contains(dr)) {
            newName.Add(dr);
        }
    }
    string[] newNameArray = newName.ToArray();
    return newNameArray;
}
或者你也可以试试

public string[] UniqueNames(string[] names1, string[] names2) {            
    return names1.Union(names2).ToArray(); ;            
}


我能看几段吗?@Austin不知道你的意思。如果你能看到建议的编辑,你就会知道我的意思。我想Austin的意思是,你在顶部的解释有点吸引眼球,可以分成几段,让它更具可读性。明白了,我不确定那里发生了什么。它在编辑模式下看起来很好,但结果是粘糊糊的。感谢zimdanen的修改:我能得到几段吗?@Austin不确定你的意思。如果你能看到建议的编辑,你就会知道我的意思。我想Austin的意思是,你在顶部的解释有点吸引眼球,可以分成几段,让它更具可读性。明白了,不知道发生了什么。它在编辑模式下看起来很好,但结果是粘糊糊的。感谢zimdanen的修复:感谢您的快速响应。这与我在第一个if部分中已经做的有什么不同,除了你将它们组合成一个调用,然后我将其分成几个部分之外?我做了同样的事情,得到了确切的结果。。gmaness您确定字符串中没有额外的空格吗?你能帮我们打印出字符串吗?谢谢你的快速回复。这与我在第一个if部分中已经做的有什么不同,除了你将它们组合成一个调用,然后我将其分成几个部分之外?我做了同样的事情,得到了确切的结果。。gmaness您确定字符串中没有额外的空格吗?你能帮我们把这些字符串打印出来吗?你不能用.Trim来删除空白。当然我可以。。我刚才举了一个例子,显然相似的刺痛可以通过你不使用的distinct和union调用扫描。修剪以删除空白。当然我可以。。我只是举了一个例子,显然相似的刺痛可以通过这个区域
nct和工会的电话这是一个很酷的想法。但是,在本例中,我在完全相同的SQL数据池上使用完全相同的类,因此数组是以相同的格式生成的。如果看不到原始字符串,我就没有主意了。你能把它们添加到你的问题中吗?这是一个很酷的想法。但是,在本例中,我在完全相同的SQL数据池上使用完全相同的类,因此数组是以相同的格式生成的。如果看不到原始字符串,我就没有主意了。你能把它们添加到你的问题中吗?答案是我编译时的一个错误,而不是实际的代码。尽管如此,我还是继续把这个标记为答案,因为它让我探索了“var”的用法,而我还没有使用它。我还喜欢清理字符串并使用ints使其更健壮的技术。答案是编译时出错,而不是实际代码。尽管如此,我还是继续把这个标记为答案,因为它让我探索了“var”的用法,而我还没有使用它。我还喜欢清理字符串并使用INT使其更健壮的技术。如果您有真正创新的内容,请仅发布旧问题的答案。如果您有真正创新的内容,请仅发布旧问题的答案。