我遇到了一个小算法问题,因此无法实现(快速)实现。 实际上,我的工作还没有完成,但是已经在减慢DataGridView的加载速度。
最初的问题: WinForms的DataGridView具有2005年以来的错误(显然直到VS2015为止尚未解决),该错误使具有相同名称的列的错误绑定不区分大小写。如果您有2列“猫”和“猫”,它们将被绑定到数据库中的同一对象(第一个找到的对象)。
无论如何,我正在使用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
算法问题: 在带有循环的列表中添加字符串。在添加之前,请检查字符串是否存在(修剪和区分大小写),如果存在,请在名称末尾添加一个空格。保持大小写不变。换句话说,通过在名称后添加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>
{
}
答案 0 :(得分:5)
尝试一个简单的 Linq :我们将分组相同的项目,然后将function api_calling() {
const URLs = fetching();
return Promise.all(URLs).then(data => {
var newPlanets = [];
for (var i = 0; i < 7; i++) {
for (var n = 0; n < 10; n++) {
newPlanets.push(data[i].results[n]);
}
}
return newPlanets;
});
}
class App extends Component {
state = { data: [] };
componentDidMount() {
api_calling().then(data => {
this.setState({ data });
});
}
render() {
// ...
}
}
空格(下层范围)添加到组。最后,我们拼合(合并)所有组。
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();
编辑::如果要保留 初始订单,我们必须将其存储(项目Console.WriteLine(string.Join(Environment.NewLine, result));
初始列表),最后排序:
cat
Cat_
cAt__
kitty
kittY_
dog
Dog_
monkey
monKey_
Monkey__
结果:
index