c#通过附加可变数量的空格来使字符串列表的元素唯一

orf*_*uit 1 c# .net-3.5

我有一点算法问题,我坚持(快速)实现.实际上我的未完成,但已经减慢了DataGridView的负载.

最初的问题:WinForms的DataGridView有一个来自2005的错误(显然还没有解决到VS2015),这使得具有相同名称的列的不正确绑定不敏感.更精确的是,如果您有2列"Cat"和"cat",它们将绑定到数据库中的相同(第一个找到)对象.

无论如何,我正在使用ITypedList和GetItemProperties()来通知DGV我想要链接的字段.这个想法(在stackoverflow上看到的地方)是在"重复大小写区分"列之后添加空格,如下所示:

"cat"   --> leave as is
"Cat"   --> needs to be come "Cat_"   _ means space
"cAt"   --> needs to become "cAt__"   __ means two spaces and so on
Run Code Online (Sandbox Code Playgroud)

算法问题:使用循环在列表中添加字符串.在添加之前,检查字符串是否存在(修剪和不区分大小写),如果是,则在名称末尾附加一个空格.保持名称不变.换句话说,通过将n个空格附加到名称来使字符串唯一.

希望我描述得很好,任何想法都赞赏.

我已经用我的变体做了一些测试并且速度受到影响,也许还有DGV正在触发GetItemProperties()回调5次或更多次的事实.

这是我的代码:

   public partial class Form1 : Form
    {

        List<string> list = new List<string>
        {
            "cat",      // the cat
            "Cat",      // first duplicate needs to become Cat_ (one space)
            "kitty",
            "kittY",
            "dog",
            "cAt",      // third duplicate needs to become cAt__ (two spaces)
            "Dog",
            "monkey",
            "monKey",
            "Monkey",
        };

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AnimalHeaders phList = new AnimalHeaders();

            for (int i = 0; i < list.Count(); i++)
            {
                string field = list.ElementAt(i);

                var caseInsenList = phList.Where(z => z.Name.Trim().Equals(field, StringComparison.OrdinalIgnoreCase));

                int count = caseInsenList.Count();

                if (count == 0) // no results
                {
                    phList.Add(new AnimalHeader { Name = field });
                }
                else // exists but can be many
                {
                    for (j = 0; j < count; j++)
                    {
                        string found = caseInsenList.ElementAt(j).Name.Trim(); // no spaces

                        if (field == found)
                            continue; // exact match, case sensitive, we already have this, skip
                        else
                        {

                        }
                    }

                }
            }

        }
    }


    public class AnimalHeader
    {
        public string Name { get; set; }
        public Type Type { get; set; }
        public int Order { get; set; }

    }

    public class AnimalHeaders : List<AnimalHeader>
    {

    }
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 5

尝试一个简单的Linq:我们相同的项目分组,然后为组中的index每个index项目添加空格(下划线).最后,我们扁平化(组合)所有群体.

  List<string> list = new List<string>() {
    "cat",      // the cat
    "Cat",      // first duplicate needs to become Cat_ (one space)
    "kitty",
    "kittY",
    "dog",
    "cAt",      // third duplicate needs to become cAt__ (two spaces)
    "Dog",
    "monkey",
    "monKey",
    "Monkey",
  };

  List<string> result = list
    .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
    .SelectMany(chunk => chunk
       .Select((item, index) => string.Format("{0}{1}", item, new string('_', index))))
    .ToList();
Run Code Online (Sandbox Code Playgroud)

演示:

Console.WriteLine(string.Join(Environment.NewLine, result));
Run Code Online (Sandbox Code Playgroud)

结果:

cat
Cat_
cAt__
kitty
kittY_
dog
Dog_
monkey
monKey_
Monkey__
Run Code Online (Sandbox Code Playgroud)

编辑:如果我们想保留最初的订单,我们必须保存它(index在初始列表中的项目),最后按订单吧:

  List<string> result = list
    .Select((value, index) => new {
      value,
      index
    })
    .GroupBy(item => item.value, StringComparer.OrdinalIgnoreCase)
    .SelectMany(chunk => chunk
       .Select((item, index) => new {
          value = string.Format("{0}{1}", item.value, new string('_', index)),
          index = item.index
        }))
    .OrderBy(item => item.index)
    .Select(item => item.value)
    .ToList();
Run Code Online (Sandbox Code Playgroud)

结果:

cat
Cat_
kitty
kittY_
dog
cAt__
Dog_
monkey
monKey_
Monkey__
Run Code Online (Sandbox Code Playgroud)