ASP.Net 映射值查找

发布于 2024-07-14 08:04:58 字数 332 浏览 6 评论 0原文

目前,在我的 ASP.Net 应用程序 web.config 中,我有一个应用程序设置,用于存储以逗号分隔的映射值列表,如下所示。 在后面的代码中,我需要根据输入值 1、2、3 等对此数据执行查找。我可以对其进行字符串拆分并循环,直到找到匹配项,或者使用 Regex 从配置字符串中提取值。

目前我正在使用正则表达式来获取映射值。 我不反对更改数据在 web.config 中的存储方式。 有没有更简单、更优雅的方法来处理这个问题?

<add key="Mappings" value="1|APP,2|TRG,3|KPK,4|KWT,5|CUT" />

Currently in my ASP.Net applications web.config I have an application setting that stores a comma delimited list of mapping values, like the one below. In the code behind I need to perform a lookup on this data based on input values 1, 2, 3 etc. I can either string split it and loop until I find a match, or use Regex to pull the value from the config string.

Currently i'm using Regex to get the mapping value. I'm not opposed to changing how the data is stored in the web.config. Is there a more simple and elegant way of handling this?

<add key="Mappings" value="1|APP,2|TRG,3|KPK,4|KWT,5|CUT" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假扮的天使 2024-07-21 08:04:58

如果您需要经常使用此查找,并且 web.config 中的字符串不经常更改,那么将字符串解析为 Dictionary 对象并将其存储在应用程序或缓存中是有意义的。

从字典中查找将快如闪电,特别是与每次解析字符串相比。

private static readonly object _MappingsLock = new object();

public static string GetMapping(int id)
{
    // lock to avoid race conditions
    lock (_MappingsLock)
    {
        // try to get the dictionary from the application object
        Dictionary<int, string> mappingsDictionary =
            (Dictionary<int, string>)Application["MappingsDictionary"];

        if (mappingsDictionary == null)
        {
            // the dictionary wasn't found in the application object
            // so we'll create it from the string in web.config
            mappingsDictionary = new Dictionary<int, string>();

            foreach (string s in mappingsStringFromWebConfig.Split(','))
            {
                string[] a = s.Split('|');
                mappingsDictionary.Add(int.Parse(a[0]), a[1]);
            }

            // store the dictionary in the application object
            // next time around we won't need to recreate it
            Application["MappingsDictionary"] = mappingsDictionary;
        }

        // now do the lookup in the dictionary
        return mappingsDictionary[id];
    }
}

// eg, get the mapping for id 4
string mapping = GetMapping(4);  // returns "KWT"

If you need to use this lookup frequently and the string in web.config doesn't change very often, then it makes sense to parse the string once into a Dictionary object and store that in the Application or Cache.

Lookups from the Dictionary will be lightning fast, especially compared to parsing the string each time.

private static readonly object _MappingsLock = new object();

public static string GetMapping(int id)
{
    // lock to avoid race conditions
    lock (_MappingsLock)
    {
        // try to get the dictionary from the application object
        Dictionary<int, string> mappingsDictionary =
            (Dictionary<int, string>)Application["MappingsDictionary"];

        if (mappingsDictionary == null)
        {
            // the dictionary wasn't found in the application object
            // so we'll create it from the string in web.config
            mappingsDictionary = new Dictionary<int, string>();

            foreach (string s in mappingsStringFromWebConfig.Split(','))
            {
                string[] a = s.Split('|');
                mappingsDictionary.Add(int.Parse(a[0]), a[1]);
            }

            // store the dictionary in the application object
            // next time around we won't need to recreate it
            Application["MappingsDictionary"] = mappingsDictionary;
        }

        // now do the lookup in the dictionary
        return mappingsDictionary[id];
    }
}

// eg, get the mapping for id 4
string mapping = GetMapping(4);  // returns "KWT"
猫卆 2024-07-21 08:04:58

只是好奇:) LINQ

string inputValue = "2";
string fromConfig = "1|APP,2|TRG,3|KPK,4|KWT,5|CUT";            
string result = fromConfig
    .Split(',')
    .Where(s => s.StartsWith(inputValue))
    .Select(s => s.Split('|')[1])
    .FirstOrDefault();

或者

Regex parseRegex = new Regex(@"((?<Key>\d)\|(?<Value>\S{3}),?)");
parseRegex.Matches(fromConfig)
    .Cast<Match>()
    .Where(m => m.Groups["Key"].Value == inputValue)
    .Select(m => m.Groups["Value"].Value)
    .FirstOrDefault();

Just curious :) What about LINQ

string inputValue = "2";
string fromConfig = "1|APP,2|TRG,3|KPK,4|KWT,5|CUT";            
string result = fromConfig
    .Split(',')
    .Where(s => s.StartsWith(inputValue))
    .Select(s => s.Split('|')[1])
    .FirstOrDefault();

Or

Regex parseRegex = new Regex(@"((?<Key>\d)\|(?<Value>\S{3}),?)");
parseRegex.Matches(fromConfig)
    .Cast<Match>()
    .Where(m => m.Groups["Key"].Value == inputValue)
    .Select(m => m.Groups["Value"].Value)
    .FirstOrDefault();
私藏温柔 2024-07-21 08:04:58

用逗号分割字符串,然后用|分割每个子字符串,将它们存储在字典中并通过键查找它。

这只是正则表达式的替代方案。 您知道他们如何评价正则表达式。

Split the string on commas, then each substring on |, store these in a Dictionary and find it by key.

That's just as an alternative to Regex. You know what they say about Regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文