Lambda将IDictionary 值转换为object []

时间:2021-07-06 19:01:46

I am trying to create a Lambda expressions that creates an object array from an IDictionary based on given named input.

我正在尝试创建一个Lambda表达式,该表达式根据给定的命名输入从IDictionary创建一个对象数组。

This is what I have so far:

这是我到目前为止:

var dic = new Dictionary<string, object>() 
        { 
            {"some", "value"}, 
            {"dateTime", DateTime.Now},
            {"someNum", 52},
            {"other", "values"}
        };

string[] inputArray = { "input", "dateTime", "someNume" };

var processedArray = inputArray.Select(i => dic.Where(d => d.Key == i).Select(kvp => kvp.Value)).ToArray();

This doesn't work, it returns an IEnumerable<object> but all the members are type KeyValuePair<string,object> just, it appears, without the key data.

这不起作用,它返回一个IEnumerable ,但所有成员都是KeyValuePair类型 只是,它出现,没有关键数据。

,object>

I think there are a few things wrong with this. I don't think I should be using dic.Select() as key entries in dictionary are unique and I am obviously referencing the value wrong or missing a way to cast each result.

我认为这有一些问题。我不认为我应该使用dic.Select()作为字典中的键条目是唯一的,我显然是引用错误的值或缺少一种方法来转换每个结果。

I am not sure how to achieve this using a single Lambda. I need to work out the Lambda as I plan to use it as a compiled Lambda.

我不确定如何使用单个Lambda实现此目的。我需要计算Lambda,因为我打算将它用作编译的Lambda。

Can someone advise me on how best to do this?

有人可以告诉我如何最好地做到这一点?

2 个解决方案

#1


I think this should do the trick.

我认为这应该可以解决问题。

var processedArray = 
    (from key in inputArray
     where dic.ContainsKey(key)
     select dic[key]).ToArray();

Or using the fluent/dot syntax:

或者使用流利/点语法:

var processedArray = inputArray
    .Where(key => dic.ContainsKey(key))
    .Select(key => dic[key])
    .ToArray(); 

If you want to still have an element in your processedArray output in the case where the dictionary misses a key (e.g. for the "input" string in your example), you could do it like this for example (using null for a missing entry in the dictionary):

如果你希望在字典错过某个键的情况下仍然在processedArray输出中有一个元素(例如,对于你的例子中的“输入”字符串),你可以这样做(例如对于缺少的条目使用null)词典):

var processedArray = inputArray
    .Select(key => dic.ContainsKey(key) ? dic[key] : null)
    .ToArray(); 

#2


You should be able to select these just by accessing the dictionary indices:

您应该只需访问字典索引就可以选择这些:

inputArray.Select(k => dic[k]);

#1


I think this should do the trick.

我认为这应该可以解决问题。

var processedArray = 
    (from key in inputArray
     where dic.ContainsKey(key)
     select dic[key]).ToArray();

Or using the fluent/dot syntax:

或者使用流利/点语法:

var processedArray = inputArray
    .Where(key => dic.ContainsKey(key))
    .Select(key => dic[key])
    .ToArray(); 

If you want to still have an element in your processedArray output in the case where the dictionary misses a key (e.g. for the "input" string in your example), you could do it like this for example (using null for a missing entry in the dictionary):

如果你希望在字典错过某个键的情况下仍然在processedArray输出中有一个元素(例如,对于你的例子中的“输入”字符串),你可以这样做(例如对于缺少的条目使用null)词典):

var processedArray = inputArray
    .Select(key => dic.ContainsKey(key) ? dic[key] : null)
    .ToArray(); 

#2


You should be able to select these just by accessing the dictionary indices:

您应该只需访问字典索引就可以选择这些:

inputArray.Select(k => dic[k]);