来自 IEnumerable> 的通用 GetOnlyKeys 的 C# 扩展方法

发布于 2024-10-31 05:22:11 字数 1432 浏览 0 评论 0 原文

我有一个 IEnumberable>我只想要键列表,但转换为所需的类型(即可能是短类型而不是整数)。这用于绑定到的自定义通用多选控件,但数据库可能需要“短”来保存。

public static IEnumerable<T> GetKeysOnly<T>(this IEnumerable<KeyValuePair<int, string>> values)
    {
        Dictionary<int, string> valuesDictionary = values.ToDictionary(i => i.Key, i => i.Value);

        List<int> keyList = new List<int>(valuesDictionary.Keys);

        // Returns 0 records cuz nothing matches
        //List<T> results = keyList.OfType<T>().ToList(); 

        // Throws exception cuz unable to cast any items
        //List<T> results = keyList.Cast<T>().ToList(); 

        // Doesn't compile - can't convert int to T here: (T)i
        //List<T> results = keyList.ConvertAll<T>(delegate(int i) { return (T)i; }); 

        throw new NotImplementedException();
    }

    public static IEnumerable<short> GetKeysOnly(this IEnumerable<KeyValuePair<int, string>> values)
    {
        Dictionary<int, string> valuesDictionary = values.ToDictionary(i => i.Key, i => i.Value);
        List<int> keyList = new List<int>(valuesDictionary.Keys);

        // Works but not flexable and requires extension method for each type
        List<short> results = keyList.ConvertAll(i => (short)i);
        return results;
    }

关于如何使我的通用扩展方法起作用有什么建议吗?
谢谢!

I have an IEnumberable> and I want only the list of Keys but cast to the needed type (i.e. perhaps short and not int). This is used in a custom generic multi-select control the binds to but the database needs potientially 'short' to save.

public static IEnumerable<T> GetKeysOnly<T>(this IEnumerable<KeyValuePair<int, string>> values)
    {
        Dictionary<int, string> valuesDictionary = values.ToDictionary(i => i.Key, i => i.Value);

        List<int> keyList = new List<int>(valuesDictionary.Keys);

        // Returns 0 records cuz nothing matches
        //List<T> results = keyList.OfType<T>().ToList(); 

        // Throws exception cuz unable to cast any items
        //List<T> results = keyList.Cast<T>().ToList(); 

        // Doesn't compile - can't convert int to T here: (T)i
        //List<T> results = keyList.ConvertAll<T>(delegate(int i) { return (T)i; }); 

        throw new NotImplementedException();
    }

    public static IEnumerable<short> GetKeysOnly(this IEnumerable<KeyValuePair<int, string>> values)
    {
        Dictionary<int, string> valuesDictionary = values.ToDictionary(i => i.Key, i => i.Value);
        List<int> keyList = new List<int>(valuesDictionary.Keys);

        // Works but not flexable and requires extension method for each type
        List<short> results = keyList.ConvertAll(i => (short)i);
        return results;
    }

Any advice how to make my generic extension method work?
Thanks!

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

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

发布评论

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

评论(1

不疑不惑不回忆 2024-11-07 05:22:11

您只想将按键转换为短键吗?

var myList = valuesDictionary.Select(x => (short)x.Key).ToList();
// A Dictionary can be enumerated like a List<KeyValuePair<TKey, TValue>>

如果你想进入任何类型,那么你会这样做:

public static IEnumerable<T> ConvertKeysTo<T>(this IEnumerable<KeyValuePair<int, string>> source)
{
     return source.Select(x => (T)Convert.ChangeType(x.Key, typeof(T)));
     // Will throw an exception if x.Key cannot be converted to typeof(T)!
}

You want to get only the keys converted to a short?

var myList = valuesDictionary.Select(x => (short)x.Key).ToList();
// A Dictionary can be enumerated like a List<KeyValuePair<TKey, TValue>>

If you want to go to any type, then you would do something like this:

public static IEnumerable<T> ConvertKeysTo<T>(this IEnumerable<KeyValuePair<int, string>> source)
{
     return source.Select(x => (T)Convert.ChangeType(x.Key, typeof(T)));
     // Will throw an exception if x.Key cannot be converted to typeof(T)!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文