用于快速查找KeyString的字典

时间:2016-03-16 15:49:37

标签: c# arrays list

我知道这可能会立刻实现,但无论如何它对我来说足够复杂,因为这是我的第一个项目:))

public class Definitions
{
    public string Name { get; set; }
    public string Id { get; set; }
    public int smallId { get; set; }
}


    public static void getDefinitions()
    {
        var files = (from name in Directory.EnumerateFiles(Folder)
                     select new Definitions
                     {
                         Name = Path.GetFileNameWithoutExtension(name),
                           Id = File.ReadLines(name).Skip(2).First()
                                }).ToArray();


        //LOOP THROUGH DATABASE ++++++++++
        for (int i = 0; i < files.Length; i++)
        {
            if ((files[i].Id) == idFromDatabase)
            {
                files[i].smallId = smallIdFromDatabase;
            }
        //LOOP THROUGH DATABASE ++++++++++


        //+++++++++++++ Dictionary CREATE++++++++++++++++
        Dictionary<string, int> _dict =
        new Dictionary<string, int>();

        foreach (Definitions item in files)
        {
            _dict.Add(item.Name,item.smallId);
        }
        //+++++++++++++ Dictionary CREATE++++++++++++++++

    return _dict;
    }

以上所有代码都需要立即执行!! 我不再需要Definitions类了。 我到处都需要那个DICTIONARY。

然后我想要一个简单的id检查方法:

        public static string GetID(string word)
        {
            // ++++++Dictionary check +++++++
            string result;
            if (_dict.TryGetValue(word, out result))
            {
            return result;
            }
            else
            {
            return null;
            }
            }

现在我只设法将字典返回到main,并直接在那里执行查找。

(当然要将_DICT赋予该查找功能,每次我进行查找都可能非常愚蠢)

我怀疑,这样做的目的是将所有内容都放在一个静态类中,我已经根据我的基本知识进行了尝试,但总是缺少一些东西,在整个地方摇摆不定。 :))

根据回答++++++++++++++++++++++++++++++++++++++++++++++++

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace Testbase
{
public class Definitions
{
    public string Name { get; set; }
    public string Id { get; set; }
    public int smallId { get; set; }
}

class Program
{
    static void Main()
    {
        _dict=GetDefinitions();
        string teststring = "StringToTest";
        Console.WriteLine(GetID(teststring));
        Console.ReadKey();
    }

    private static Dictionary<string, int> _dict = new Dictionary<string, int>();

    public static Dictionary<string, int> getDefinitions()
    {
    if (_dict.Count >= 0) return _dict;
        var files = (from name in Directory.EnumerateFiles(Settings.Folder)
                     select new Definitions
                     {
                         Name = Path.GetFileNameWithoutExtension(name),
                         Id = File.ReadLines(name).Skip(2).First().Replace("</ID>", "").Replace("<ID>", "").Trim()
                     }).ToArray();
        //++++++++++++++++++ Loop trough databse ++++++++++++++++++++++++
        for (int i = 0; i < files.Length; i++)
        {
            if ((files[i].Id) == idFromDatabase)
            {
                files[i].smallId = smallIdFromDatabase;
            }
        }
        //++++++++++++++++++ Loop trough databse ++++++++++++++++++++++++

        foreach (Definitions item in files)
        {
            _dict.Add(item.Name, item.smallId);
        }

        return _dict;
    }

    public static int GetID(string word)
    {
        int outp;
        if (_dict.TryGetValue(word, out outp))
        {
            return outp;
        }
        else
        {
            return 0;
        }
    }
}
}

问题是,只有在方法GetID:

中创建新词典时,它才有效
    public static int GetID(string word)
    {

但这并不好,因为我经常需要查找

已解决####################################### 当然我需要_dict = getDefinitions();在主要的TOP。 然后我可以在任何地方调用GETID,而不必每次都创建一个新的DICT对象 或者至少这就是我的想法...... 非常感谢您的帮助

伟大的论坛!!!!!

1 个答案:

答案 0 :(得分:0)

此处我对您的代码进行了更改:

1)我将getDefinitions()的签名从void更改为Dictionary。

2)声明了一个私有字段_dict,它将在类级别保存字典值,而不是在方法中保存变量。

3)私有字段是静态的。

4)我在getDefinitions()方法的开头做了一个简单的检查,检查字段_dict是否已填充,然后使用它。否则去阅读文件。

public class Definitions
{
    public string Name { get; set; }
    public string Id { get; set; }
    public int smallId { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
    }
    // DECLARE THE DICTIONARY AT CLASS LEVEL.
    private static Dictionary<string, int> _dict = new Dictionary<string, int>();

    // int idFromDatabase;
    // int smallIdFromDatabase;
    public static Dictionary<string, int> getDefinitions()
    {
        // RETURN DICTIONARY IF WE HAVE VALUE. DON'T NEED TO GET FILES!
        if (_dict.Count >= 0) return _dict;
        var files = (from name in Directory.EnumerateFiles(Folder)
            select new Definitions
            {
                Name = Path.GetFileNameWithoutExtension(name),
                Id = File.ReadLines(name).Skip(2).First()
            }).ToArray();


        //LOOP THROUGH DATABASE ++++++++++
        for (int i = 0; i < files.Length; i++)
        {
            if ((files[i].Id) == idFromDatabase)
            {
                files[i].smallId = smallIdFromDatabase;
            }
            //LOOP THROUGH DATABASE ++++++++++


            //+++++++++++++ Dictionary CREATE++++++++++++++++        

            foreach (Definitions item in files)
            {
                _dict.Add(item.Name, item.smallId);
            }
            //+++++++++++++ Dictionary CREATE++++++++++++++++   
            return _dict;
        }
    }

    public static int GetID(string word)
    {
        // ++++++Dictionary check +++++++
        int result;
        if (_dict.TryGetValue(word, out result))
        {
            return result;
        }
        return -1;
    }
}