Extracting a Dictionary<TKey, double> from a Dictionary<TKey, string> instance - c#

I have a generic Dictionary where TValue is of type String (Dictionary<int, string>). I chose to use string as the value type because the data was loaded from an Xml file where the source values can be character or numeric data types (I suppose Object would've been an acceptable TValue type too, but even then this question would be equally applicable).
The character data types also have importance, so excluding them outright isn't an option.
I'd like to extract a subset of this Dictionary<int, double>. In other words, I'd like the subset of the dictionary where the values are numeric.
Right now I'm doing it like this:
Dictionary<int, string> myDictionary;
// Do some loading.
var numericData = myDictionary.Where(kvp => Double.TryParse(kvp.Value, out temp)
This approach is awfully ugly and doesn't get me the result as a Dictionary<int, double> Can anyone offer other ways to improve this?
Thanks!

The code you've given is not only ugly - it will fail with an InvalidCastException at execution time. I suspect you actually want:
var numericData = myDictionary
.Select(kvp => {
double value;
return new { kvp.Key,
Value = double.TryParse(kvp.Value, out value)
? value : (double?) null
};
})
.Where(pair => pair.Value != null)
.ToDictionary(pair => pair.Key, pair => pair.Value.Value);
And yes, that's ugly - but:
It avoids parsing the value more than once
It avoids putting side-effects in your query
You can make it slightly cleaner but less efficient if you're happy to parse twice:
var numericData = myDictionary
.Where(kvp => { double tmp; return double.TryParse(kvp.Value, out tmp); })
.ToDictionary(pair => pair.Key, pair => double.Parse(pair.Value));
Or (more cleanly) you could create a separate method:
public static double? TryParseNullableDouble(string text)
{
double value;
return double.TryParse(text, out value) ? value : (double?) null;
}
Then the first version becomes:
var numericData = myDictionary
.Select(kvp => new { kvp.Key, TryParseNullableDouble(kvp.Value) })
.Where(pair => pair.Value != null)
.ToDictionary(pair => pair.Key, pair => pair.Value.Value);

You can just create a new dictionary using the temp variable which contains the double value - this exploits the fact that the enumeration and addition to the dictionary is done item by item so temp contains the correct double value:
double temp = 0;
var numDict = myDictionary.Where(kvp => Double.TryParse(kvp.Value, out temp))
.ToDictionary( x=> x.Key, x=> temp);

Related

Efficient way to create a new list based of the differences in values in 2 dictionaries?

I currently have 2 strings that are formatted as an XML that are later converted into dictionaries for comparison.
So, I have a 2 Dictionary<string, object>, dict1 and dict2, that I need to compare. I need to:
Add the key to a list of strings if the values of these two dictionaries do not match
Add the key of dict2 to the list if dict1 does not contain this key
Currently, I have a simple foreach loop
foreach (string propName in dict2.Keys)
{
string oldDictValue;
string newDicValue = dict1[propName].ToString();
if (dict1.ContainsKey(propName))
{
oldDictValue = dict2[propName].ToString();
if (oldDictValue != newDicValue)
list.Add(propName);
}
else
{
list.Add(propName);
}
}
I would like to a faster solution to this problem if possible?
I don't claim that this is any faster, but it should be on par and it's less code:
List<string> list =
dict2
.Keys
.Where(k => !(dict1.ContainsKey(k) && dict1[k].Equals(dict2[k])))
.ToList();
I did do some testing with this:
List<string> list =
dict2
.Keys
.AsParallel()
.Where(k => !(dict1.ContainsKey(k) && dict1[k].Equals(dict2[k])))
.ToList();
That produced a significantly faster run.
Here's how I produced my test data:
var dict1 = Enumerable.Range(0, 10000000).Select(x => Random.Shared.Next(2000000)).Distinct().ToDictionary(x => x.ToString(), x => (object)Random.Shared.Next(20));
var dict2 = Enumerable.Range(0, 10000000).Select(x => Random.Shared.Next(2000000)).Distinct().ToDictionary(x => x.ToString(), x => (object)Random.Shared.Next(20));
You could make it faster by avoiding to get separately the dict1[propName] and the dict2[propName]. You could get the value along with the key, either by enumerating directly the KeyValuePairs stored in the dictionary, or by calling the TryGetValue method:
foreach (var (key, value2) in dict2)
{
if (!dict1.TryGetValue(key, out var value1)
|| value1.ToString() != value2.ToString())
{
list.Add(key);
}
}

LinQ ofType in Value

I have the following Dictionary:
public Dictionary<string,object> Items;
Now I need to get all Items where the Value of the Dictionary-item is from a specific type. (e.g. "int")
var intValues = Items.OfType<KeyValuePair<string,int>> simply does not work.
Code without LinQ would be something like:
var intValues=new Dictionary<string,int>()
foreach (var oldVal in Items) {
if (oldVal.Value is int) {
intValues.add(oldVal.Key, oldVal.Value);
}
}
(Update) my example should show the basic idea. But if possible I would avoid to create a new Dictionary as a result.
The direct translation of your foreach would be the following in LINQ:
var intValues = Items.Where(item => item.Value is int)
.ToDictionary(item => item.Key, item => (int)item.Value);
So basically, you filter first for where the item.Value is an int, and then you create a dictionary from it using ToDictionary where you cast those values to int to make sure that the resulting dictionary is a Dictionary<string, int>. Since we filtered non-integers already, this type cast will always succeed.
You can use the is operator on the Value property:
var intValues = Items.Where(x => x.Value is int);
If you want an actual Dictionary<string,int> at the end just add:
.ToDictionary(v=> v.Key, v=> (int)v.Value)
Try with this:
var intValue = Items
.Where(x => x.Value is int) // filter per Value is int
.ToDictionary(x => x.Key, x => (int)x.Value); // create a new dictionary converting Value to int
You can do
var result = Items.Where(x => x.Value is int)
.ToDictionary(x => x.Key, x => x.Value);

filtering out null values while converting datatable to dictionary using linq

I am converting a datatable to dictionary using below code:
Dictionary<string, decimal> dict = dt.AsEnumerable()
.Select(dr => new { Key = dr["key_col"], Value = dr["value_col"] })
.ToDictionary(kvp => (string)kvp.Key, kvp => (decimal)kvp.Value);
but it throws me an 'input string not in correct format' exception. I am pretty sure its because of some null rows present in the datatable. how can I filter out those null rows from this code?
I'm not sure about the exception, but if you want to remove all null-values you can add a Where-clause.
Dictionary<string, decimal> dict = dt.AsEnumerable()
.Where(dr => dr["key_col"] != null && !string.IsNullOrEmpty(dr["key_col"].ToString()))
.Select(dr => new { Key = dr["key_col"], Value = dr["value_col"] })
.ToDictionary(kvp => (string)kvp.Key, kvp => (decimal)kvp.Value);
You may also want to try !string.IsNullOrEmpty(dr["key_col"] as string) in the Where-clause. But I've have experienced some problems with it when casting column values. Therefore I tend to use dr["key_col"] != null && !string.IsNullOrEmpty(dr["key_col"].ToString()) instead.
The only string in the query is the key of the dictionary. The exception suggests that you're trying to convert it to a numeric type later(f.e. int.Parse).
You should do this in the query and use a Dictionary<int, decimal>. For example:
int key;
Dictionary<int, decimal> dict = dt.AsEnumerable()
.Where(dr => int.TryParse(dr.Field<string>("key_col"), out key))
.Select(dr => new { Key = key, Value = dr.Field<decimal>("value_col") })
.ToDictionary(x=> x.Key, x=> x.Value);
If it's actually not an int but a different type, f.e.double use double.TryParse

How can I Remove items from dictionary using lambda expression

I am not into LINQ solutions,
I am using simple predicat to determine if the key should be removed,
For example if the dictionary is construct like Dictionary<int, int>,
so how should I remove all the entries with negative data
I am prefer to use the same dictionary, not to create new one,
I don't have preformance issues
Is there a way to do it, without using LINQ, but using Lambda expressions?
I didn't want solutions in LINQ because no one is using them in my project, didn't want to be the first.., but because I saw the LINQ solutions look better, I will use them them..
The simplest way is probably to create a new dictionary, if that's okay for you:
var newDictionary = oldDictionary.Where(pair => pair.Value >= 0)
.ToDictionary(pair => pair.Key,
pair => pair.Value);
If you have to mutate the existing dictionary (e.g. because several other objects have reference to the same dictionary) you'd need to build a list of keys to remove, then remove them afterwards:
var toRemove = dictionary.Where(pair => pair.Value < 0)
.Select(pair => pair.Key)
.ToList();
foreach (var key in toRemove)
{
dictionary.Remove(key);
}
EDIT: I've just noticed the first sentence: "I am not into LINQ solutions". If that means you don't want to use a LINQ solution, here's the by-hand version:
List<int> toRemove = new List<int>();
foreach (KeyValuePair<int, int> pair in dictionary)
{
if (pair.Value < 0)
{
toRemove.Add(pair.Key);
}
}
foreach (var key in toRemove)
{
dictionary.Remove(key);
}
... but if you can use LINQ, I'd encourage you do. My second solution is equivalent to the "by-hand" version, but more readable IMO.
By merely using lambda expression:
foreach (var i in myDict.Where(d => (d.Value < 0 || d.key <0)).ToList() )
{
myDict.Remove(i.Key);
}
var toRemove = dict.Keys.Where(predicate).ToArray();
foreach (var key in toRemove) {
dict.Remove(key);
}
Well if you add
namespace MMExtensions
{
public static class DictionaryExtensions
{
public delegate bool Predicate<TKey, TValue>(KeyValuePair<TKey, TValue> d);
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Filter<TKey, TValue>(
this Dictionary<TKey, TValue> hashtable, Predicate<TKey, TValue> p)
{
foreach (KeyValuePair<TKey, TValue> value in hashtable.ToList().Where(value => !p(value)))
hashtable.Remove(value.Key);
}
}
}
And you had some dataset as dictionary:
Dictionary<string, int> d =
new Dictionary<string, int> {{"v", -3}, {"val1", 1}, {"val2", 2}};
Then you could use:
d.Filter(delegate(KeyValuePair<string, int> kv) { return kv.Value >= 0; });
d.Filter(kv => kv.Value >= 0);// or as lambda
Do you want to remove the items from that dictionary, or are you happy to use a new dictionary without those items included?
var d = new Dictionary<int,int>();
var newDict = d.Where(entry => entry.Value >= 0).ToDictionary(entry => entry.Key, entry => entry.Value);
Easiest one:
Dictionary<long, long> dict...
Dictionary<long, long> result = dict.Were(x => x.Value >= 0).ToDictionary(x => x.Key, x => x.Value);
Or just loop over all in 'for' in reverse order and remove invalid ones.
I know you said you are not into Linq, but I could not contain myself with the following solution, plus it is still useful if you read the title of your question. This is probably the most elegant solution to your problem:
dictionary.Where(pair => pair.Value < 0)
.Select(pair => {
dictionary.Remove(pair.Key);
return pair.Key;
});

Convert an IOrderedEnumerable<KeyValuePair<string, int>> into a Dictionary<string, int> [duplicate]

This question already has answers here:
Recreating a Dictionary from an IEnumerable<KeyValuePair<>>
(2 answers)
Closed last year.
I was following the answer to another question, and I got:
// itemCounter is a Dictionary<string, int>, and I only want to keep
// key/value pairs with the top maxAllowed values
if (itemCounter.Count > maxAllowed) {
IEnumerable<KeyValuePair<string, int>> sortedDict =
from entry in itemCounter orderby entry.Value descending select entry;
sortedDict = sortedDict.Take(maxAllowed);
itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */);
}
Visual Studio's asking for a parameter Func<string, int> keySelector. I tried following a few semi-relevant examples I've found online and put in k => k.Key, but that gives a compiler error:
'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,int>>'
does not contain a definition for 'ToDictionary' and the best
extension method overload
'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>,
System.Func<TSource,TKey>)' has some invalid arguments
You are specifying incorrect generic arguments. You are saying that TSource is string, when in reality it is a KeyValuePair.
This one is correct:
sortedDict.ToDictionary<KeyValuePair<string, int>, string, int>(pair => pair.Key, pair => pair.Value);
with short version being:
sortedDict.ToDictionary(pair => pair.Key, pair => pair.Value);
I believe the cleanest way of doing both together: sorting the dictionary and converting it back to a dictionary would be:
itemCounter = itemCounter.OrderBy(i => i.Value).ToDictionary(i => i.Key, i => i.Value);
The question is too old but still would like to give answer for reference:
itemCounter = itemCounter.Take(maxAllowed).OrderByDescending(i => i.Value).ToDictionary(i => i.Key, i => i.Value);

Categories