小编Chr*_*son的帖子

使用字符串查找C#字典

我想通过使用字符串找到一个字典,以便将其复制到一个新的字典中 - 看起来它应该很简单,但我不确定语法.

基本上它只是:

    // the object's myName corresponds to an existing dictionary
    string stringdic = myName;      

    // here's where I try to create a new dictionary containing the values of the existing dictionary
    Dictionary<string, int> mySkills = new Dictionary<string, int>(myName);
Run Code Online (Sandbox Code Playgroud)

这里的目标只是创建一个具有给定字符串作为其"名称"的对象,并从名称知道它应该使用的一组字典中的哪一个 - 例如,如果myName = Bob,我想访问字典Bob在这个脚本中.

因此,引用预先存在的字典以获得其键/值的一些其他方式也将起作用.速度不是一个大问题,但我想保持代码简单.谢谢!

c# dictionary

2
推荐指数
1
解决办法
90
查看次数

在数组上使用System.Linq中的.Any来查找字符串

我试图找出我的数组中的特定单元格是否包含给定字符串的一部分.因为数组不使用.Contains我正在使用.Any,但似乎我对.Any如何工作的理解太朦胧,无法正确使用.

public void ProcessCSV (string typeName) {

    for (int y = 0; y < CSVReader.grid.GetUpperBound(1); y++) {
        if (CSVReader.grid[0,y] != null) {
            if ((CSVReader.grid[0,y].Any(s => typeName.Contains(s)))) {
                  // (add it to a new list)
Run Code Online (Sandbox Code Playgroud)

所以,我喂像"猪肉"的字符串,"农超对接"等.有时它似乎很好地工作,例如,如果typeName为"农场"我只拿回从数组行,其中包括在[0,Y]该字符串.但在其他时候,如果我使用Farm,另一个字符串或随机乱码,它只返回任何字符串的任何一行.

我打电话的时候实际发生了什么.这样吗?我可以使用一些替代方法吗?

c# linq arrays string

1
推荐指数
1
解决办法
8382
查看次数

标签 统计

c# ×2

arrays ×1

dictionary ×1

linq ×1

string ×1