Foreach object loop - c#

My already code:
foreach (KeyValuePair<string, object> keyValuePair in items)
{
if (keyValuePair.Key == "animals")
{
Dictionary<string, object> animal = new Dictionary<string, object>();
animal.Add(keyValuePair.Key, keyValuePair.Value);
object animalObject = keyValuePair.Value;
foreach (??? in animalObject)
{
}
}
object fajny = keyValuePair.Value;
result2 = fajny as ArrayCollection;
}
I want to loop Dictionary elements from animalObject object:
http://scr.hu/0tgp/6dteu
http://screenshu.com/static/uploads/temporary/1h/1p/05/3p9fyc.jpg
I don't have an idea how do this. I cant search help anywhere.

You can try to cast to IEnumerable, but I would suggest trying to retain strong typing.
foreach (KeyValuePair<string, object> keyValuePair in items)
{
if (keyValuePair.Key == "animals")
{
Dictionary<string, object> animal = new Dictionary<string, object>();
animal.Add(keyValuePair.Key, keyValuePair.Value);
IEnumerable animalObject = keyValuePair.Value as IEnumerable;
if(animalEnumerable != null)
{
foreach (object animalObj in animalEnumerable)
{
}
}
}
}

are you trying to loop over the values in your dictionary: animal?
foreach (object animalObject in animal.Values)
{
}

Related

Managing lots of object properties

I have some classes which have a large number of properties of varying types: e.g. 15 properties, some string, some int, some enum and some custom types.
In order to "mange" and "interact" with them without requiring lots of constructor overloads and stuff I am currently using a Dictionary<string, string> in my getters and setters. For example:
{
public IDictionary<string, string> Attributes
{
get
{
Dictionary<string, string> atts = new Dictionary<string, string>(15);
atts.Add("Prop1", Prop1.ToString());
atts.Add("Prop2", Prop2);
...
}
set
{
IDictionary<string, string> atts = value;
foreach (KeyValuePair<string, string> att in atts)
switch (att.Key) // Yeah I know latest C# has more concise syntax
{
case "Prop1":
Prop1 = att.Value;
break;
...
}
}
}
}
I did also come up with a slightly less verbose way using reflection:
public Dictionary<string, object> GetProperties()
{
Dictionary<string, object> dict = new Dictionary<string, object>();
Type t = typeof(MyClass);
PropertyInfo[] props = t.GetProperties();
foreach (var prop in props)
{
dict.Add(prop.Name, prop.GetValue(this));
}
return dict;
}
public void SetProperties(Dictionary<string, object> props)
{
foreach (var prop in props)
{
var propToSet = this.GetType().GetProperty(prop.Key);
propToSet.SetValue(this, prop.Value);
}
}
Obviously I can also use constructor initialization like:
MyClass myClass = new()
{
Prop1 = ...,
Prop2 = ...
...
}
I just wonder if there's any other elegant solutions out there?
You can convert the object to Json then Deserialize it to dictionary
public static Dictionary<string, object> ToDictionary(object model)
{
var serializedObj = JsonModelSerializer.Serialize(model);
return JsonModelSerializer.Deserialize<Dictionary<string, object>>(serializedObj);
}

Converting object to Dictionary<PropertyName, Value>

I have a use case, where I have to show any List of objects List<T> objects as a table. So, I have implemented a function, which prepares the data in usable format:
public static Dictionary<Guid, Dictionary<string, object>> PrepareList<T>(List<T> Items, List<string> RelevantProperties)
{
var res = new Dictionary<Guid, Dictionary<string, object>>();
var propDicSplitted = new Dictionary<string, List<string>>();
foreach (string relevantProperty in RelevantProperties)
{
if (relevantProperty.Contains("."))
{
var split = relevantProperty.Split('.');
if(!propDicSplitted.ContainsKey(split[0]))
{
propDicSplitted.Add(split[0], new List<string>());
}
propDicSplitted[split[0]].Add(relevantProperty);
}
}
foreach (T item in Items)
{
var itemPropDic = item.ToDictionary();
var itemId = (Guid)itemPropDic["ID"];
if (!res.ContainsKey(itemId))
{
res.Add(itemId, new Dictionary<string, object>());
}
foreach(string relevantProperty in RelevantProperties)
{
if (itemPropDic.ContainsKey(relevantProperty)) {
res[itemId].Add(relevantProperty, itemPropDic[relevantProperty]);
}
}
foreach(string subObjectName in propDicSplitted.Keys)
{
foreach (string relevantSubProperty in propDicSplitted[subObjectName])
{
res[itemId].Add(relevantSubProperty, GetNestedPropertyValue(itemPropDic, relevantSubProperty.Split('.')));
}
}
}
return res;
}
private static object GetNestedPropertyValue(IDictionary<string, object> Obj, string[] PropertiesPath)
{
if (PropertiesPath.Length == 1)
return Obj[PropertiesPath[0]];
return GetNestedPropertyValue(Obj[PropertiesPath[0]].ToDictionary(), PropertiesPath.Skip(1).ToArray());
}
where, List<T> items is the list of items to be shown in table and List<string> RelevantProperties are the property names, which are relevant for the table. These can be nested via '.' (eg. when the Orders object has a Customer object as a property and I need to show the customer name, then it will be possible to add value to RelevantProperties as "Customer.Name", which then will be extracted).
The .ToDictionary() method is extended to provide the access to the properties as in dictionary:
public static IDictionary<string, object> ToDictionary(this object source, bool ignoreComplexTypes = false)
{
return source.ToDictionary<object>(ignoreComplexTypes);
}
public static IDictionary<string, T> ToDictionary<T>(this object source, bool ignoreComplexTypes)
{
if (source == null)
throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
var dictionary = new Dictionary<string, T>();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
AddPropertyToDictionary<T>(property, source, dictionary, ignoreComplexTypes);
return dictionary;
}
private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary, bool ignoreComplexTypes)
{
object value = property.GetValue(source);
if (IsOfType<T>(value) && !(ignoreComplexTypes))
dictionary.Add(property.Name, (T)value);
}
private static bool IsOfType<T>(object value)
{
return value is T;
}
However, the problem is that the function PrepareList<T>() is too slow and it takes 2 minutes for a list with 10000 items and 8 relevant properties. I need a hint on how to optimize this. Any suggestions are appreciated.

Nested foreach to LINQ in multi-level dictionary

I would like to simplify below nested foreach loops using LINQ but couldn't figure out the way. I guess I can use SelectMany using lambda but not sure. I want to create list of objects of ClassA after this nested iteration. Any help is appreciated:
public List<ClassA> GetLists(Dictionary<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> groups)
{
var retOutput = new List<ClassA>();
foreach (KeyValuePair<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> group1 in groups)
{
foreach (KeyValuePair<IEnumerable, Dictionary<string, ClassB>> group2 in group1.Value)
{
foreach (KeyValuePair<string, ClassB> group3 in group2.Value)
{
GetList(retOutput, group1.Key,
group2.Key,
group3);
}
}
}
return retOutput;
}
private static void GetList(List<ClassA> retOutput,
string group1Key,
IEnumerable group2Key,
KeyValuePair<string, ClassB> group3)
{
List<List<string>> itemIdsLists = group3.Value.ItemId.IntoChunks(2000);
foreach (var itemIdList in itemIdsLists)
{
var currentRequest = new ClassA
{
TransactionType = group1Key,
Filters = new Dictionary<string, object>(),
ItemIds = new List<string>(),
PropStreamsDict = new Dictionary<string, Tuple<long, string>>()
};
if (group2Key is Dictionary<string, object>)
{
currentRequest.Filters = (Dictionary<string, object>)group2Key;
}
currentRequest.PropStreamsDict.Add(group3.Key, Tuple.Create(group3.Value.StreamId,
group3.Value.Uom));
currentRequest.ItemIds.AddRange(itemIdList);
retOutput.Add(currentRequest);
}
}
You should use SelectMany to make nested foreach.
Here what I come up with:
public List<ClassA> GetLists(Dictionary<string, Dictionary<IEnumerable, Dictionary<string, ClassB>>> groups)
{
return groups
.SelectMany(grp1 => grp1.Value
.SelectMany(grp2 => grp2.Value
.SelectMany(grp3 => grp3.Value.ItemId
.IntoChunks(2000)
.Select(itemIdList =>
new ClassA
{
TransactionType = grp1.Key,
Filters = grp2.Key is Dictionary<string, object> ?
(Dictionary<string, object>)grp2.Key :
new Dictionary<string, object>(),
ItemIds = new List<string>(itemIdList),
PropStreamsDict = new Dictionary<string, Tuple<long, string>>
{
{ grp3.Key, Tuple.Create(grp3.Value.StreamId, grp3.Value.Uom) }
}
}
)
)
)
)
.ToList();
}
You didn't post your ClassA and ClassB so I had to guess.

c# List<Dictionary<string,string>> add unique dictionaries

I have a list of dictionary:
List<Dictionary<string, string>> items = new List<Dictionary<string, string>>();
foreach (var group in groupedItems)
{
foreach (var item in group)
{
Dictionary<string, string> newItem = new Dictionary<string, string>();
newItem.Add("name", item.Name);
newItem.Add("value", item.Value);
}
}
items.Add(newItem);
Basically when I loop through the grouped items, I create a Dictionary where the key is the item.Name and value is item.Value. In a grouped case, this will result in duplicate dictionaries to the list.
How can I avoid adding duplicate Dictionary to this List?
I have a foreach loop and I want to add some items once.
First thing that comes to mind would be to create your own class which extends Dictionary<string, string> and implement your own version of GetHashCode() and Equals:
public class MyDictionary : Dictionary<string, string>
{
public override int GetHashCode()
{
...
}
public override bool Equals(object source)
{
...
}
}
Within the Equals you implement your equality mechanism, and in GetHashCode you implement a mechanism which yields the same numeric value for two dictionaries which are the same, according to your equality criteria.
Then, instead of a List<Dictionary<string, string>>, you use a HashSet<MyDictionary>. Since sets do not allow duplicates, you should end up with a collection of unique dictionary collections.
I solved this in this way:
I created a new dictionary:
Dictionary<string, string> control = new Dictionary<string, string>();
And then I just do like this:
Dictionary<string, string> newItem = new Dictionary<string, string>();
newItem.Add("name", item.Name);
newItem.Add("value", item.Value);
if (!control.ContainsKey(item.Name))
{
control.Add(item.Name);
items.Add(newItem);
}
You can implement your own EqualityComparer to determine if two dictionaries are equal:
class EqualityComparer<Dictionary<string, string>> : IEqualityComparer<Dictionary<string, string>>
{
public bool Equals(Dictionary<string, string> x, Dictionary<string, string> y)
{
// your code here
}
public int GetHashCode(Dictionary<string, string> obj)
{
// your code here
}
}
Now you may use this comparer within a check for existance of a new item:
foreach (var g in groupedItems)
{
Dictionary<string, string> newItem = new Dictionary<string, string>();
foreach(var item in g)
{
newItem.Add("name", item.Name);
newItem.Add("value", item.Value);
}
if (!items.Contains(newItem, new EqualityComparer()) items.Add(newItem);
}
Thus there is no need to create a new implementation of Dictionary.

How to convert object to Dictionary<TKey, TValue> in C#?

How do I convert a dynamic object to a Dictionary<TKey, TValue> in C# What can I do?
public static void MyMethod(object obj)
{
if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
{
// My object is a dictionary, casting the object:
// (Dictionary<string, string>) obj;
// causes error ...
}
else
{
// My object is not a dictionary
}
}
The above answers are all cool. I found it easy to json serialize the object and deserialize as a dictionary.
var json = JsonConvert.SerializeObject(obj);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
I don't know how performance is effected but this is much easier to read. You could also wrap it inside a function.
public static Dictionary<string, TValue> ToDictionary<TValue>(object obj)
{
var json = JsonConvert.SerializeObject(obj);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, TValue>>(json);
return dictionary;
}
Use like so:
var obj = new { foo = 12345, boo = true };
var dictionary = ToDictionary<string>(obj);
I use this helper:
public static class ObjectToDictionaryHelper
{
public static IDictionary<string, object> ToDictionary(this object source)
{
return source.ToDictionary<object>();
}
public static IDictionary<string, T> ToDictionary<T>(this object source)
{
if (source == null)
ThrowExceptionWhenSourceArgumentIsNull();
var dictionary = new Dictionary<string, T>();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
AddPropertyToDictionary<T>(property, source, dictionary);
return dictionary;
}
private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary)
{
object value = property.GetValue(source);
if (IsOfType<T>(value))
dictionary.Add(property.Name, (T)value);
}
private static bool IsOfType<T>(object value)
{
return value is T;
}
private static void ThrowExceptionWhenSourceArgumentIsNull()
{
throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
}
}
the usage is just to call .ToDictionary() on an object
Hope it helps.
public static KeyValuePair<object, object > Cast<K, V>(this KeyValuePair<K, V> kvp)
{
return new KeyValuePair<object, object>(kvp.Key, kvp.Value);
}
public static KeyValuePair<T, V> CastFrom<T, V>(Object obj)
{
return (KeyValuePair<T, V>) obj;
}
public static KeyValuePair<object , object > CastFrom(Object obj)
{
var type = obj.GetType();
if (type.IsGenericType)
{
if (type == typeof (KeyValuePair<,>))
{
var key = type.GetProperty("Key");
var value = type.GetProperty("Value");
var keyObj = key.GetValue(obj, null);
var valueObj = value.GetValue(obj, null);
return new KeyValuePair<object, object>(keyObj, valueObj);
}
}
throw new ArgumentException(" ### -> public static KeyValuePair<object , object > CastFrom(Object obj) : Error : obj argument must be KeyValuePair<,>");
}
From the OP:
Instead of converting my whole Dictionary, i decided to keep my obj
dynamic the whole time. When i access the keys and values of my
Dictionary with a foreach later, i use foreach(dynamic key in
obj.Keys) and convert the keys and values to strings simply.
Another option is to use NewtonSoft.JSON.
var dictionary = JObject.FromObject(anObject).ToObject<Dictionary<string, object>>();
If you don't mind LINQ Expressions;
public static Dictionary<string, object> ConvertFromObjectToDictionary(object arg)
{
return arg.GetType().GetProperties().ToDictionary(property => property.Name, property => property.GetValue(arg));
}
this should work:
for numbers, strings, date, etc.:
public static void MyMethod(object obj)
{
if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
{
IDictionary idict = (IDictionary)obj;
Dictionary<string, string> newDict = new Dictionary<string, string>();
foreach (object key in idict.Keys)
{
newDict.Add(key.ToString(), idict[key].ToString());
}
}
else
{
// My object is not a dictionary
}
}
if your dictionary also contains some other objects:
public static void MyMethod(object obj)
{
if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
{
IDictionary idict = (IDictionary)obj;
Dictionary<string, string> newDict = new Dictionary<string, string>();
foreach (object key in idict.Keys)
{
newDict.Add(objToString(key), objToString(idict[key]));
}
}
else
{
// My object is not a dictionary
}
}
private static string objToString(object obj)
{
string str = "";
if (obj.GetType().FullName == "System.String")
{
str = (string)obj;
}
else if (obj.GetType().FullName == "test.Testclass")
{
TestClass c = (TestClass)obj;
str = c.Info;
}
return str;
}
public static void MyMethod(object obj){
Dictionary<string, string> dicEditdata = data as Dictionary<string, string>;
string abc=dicEditdata["id"].ToString();}
suppose---
if you place the cursor over the object(obj) while debugging and
if you get an object with the value {['id':'ID1003']}
then you can use the value as
string abc=dicEditdata["id"].ToString();
Assuming key can only be a string but value can be anything try this
public static Dictionary<TKey, TValue> MyMethod<TKey, TValue>(object obj)
{
if (obj is Dictionary<TKey, TValue> stringDictionary)
{
return stringDictionary;
}
if (obj is IDictionary baseDictionary)
{
var dictionary = new Dictionary<TKey, TValue>();
foreach (DictionaryEntry keyValue in baseDictionary)
{
if (!(keyValue.Value is TValue))
{
// value is not TKey. perhaps throw an exception
return null;
}
if (!(keyValue.Key is TKey))
{
// value is not TValue. perhaps throw an exception
return null;
}
dictionary.Add((TKey)keyValue.Key, (TValue)keyValue.Value);
}
return dictionary;
}
// object is not a dictionary. perhaps throw an exception
return null;
}
I've done something like this and works for me.
using System.ComponentModel;
var dictionary = new Dictionary<string, string>();
foreach (var propDesc in TypeDescriptor.GetProperties(Obj))
{
if (!string.IsNullOrEmpty(propDesc.GetValue(Obj)))
{
dictionary.Add(propDesc.Name, propDesc.GetValue(Obj));
}
}
Also, another alternative and innovative solution is here.
var dictionary = new System.Web.Routing.RouteValueDictionary(Obj);
I hope this could work :)
// obj = new { a = "string", b = 0, c = true };
static Dictionary<string, object> ToDictionary(object obj)
{
int i = 0;
var props = obj.GetType().GetProperties();
return props.ToDictionary(k => props[i].Name, v => props[i++].GetValue(obj));
}
This code securely works to convert Object to Dictionary (having as premise that the source object comes from a Dictionary):
private static Dictionary<TKey, TValue> ObjectToDictionary<TKey, TValue>(object source)
{
Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>();
TKey[] keys = { };
TValue[] values = { };
bool outLoopingKeys = false, outLoopingValues = false;
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
{
object value = property.GetValue(source);
if (value is Dictionary<TKey, TValue>.KeyCollection)
{
keys = ((Dictionary<TKey, TValue>.KeyCollection)value).ToArray();
outLoopingKeys = true;
}
if (value is Dictionary<TKey, TValue>.ValueCollection)
{
values = ((Dictionary<TKey, TValue>.ValueCollection)value).ToArray();
outLoopingValues = true;
}
if(outLoopingKeys & outLoopingValues)
{
break;
}
}
for (int i = 0; i < keys.Length; i++)
{
result.Add(keys[i], values[i]);
}
return result;
}
This way for object array to Dictionary<string, object> List coverting
object[] a = new object[2];
var x = a.Select(f => (Dictionary<string, object>)f).ToList();
This way for single object to Dictionary<string, object> coverting
object a = new object;
var x = (Dictionary<string, object>)a;
You can create a generic extension method and then use it on the object like:
public static class Extensions
{
public static KeyValuePair<TKey, TValue> ToKeyValuePair<TKey, TValue>(this Object obj)
{
// if obj is null throws exception
Contract.Requires(obj != null);
// gets the type of the obj parameter
var type = obj.GetType();
// checks if obj is of type KeyValuePair
if (type.IsGenericType && type == typeof(KeyValuePair<TKey, TValue>))
{
return new KeyValuePair<TKey, TValue>(
(TKey)type.GetProperty("Key").GetValue(obj, null),
(TValue)type.GetProperty("Value").GetValue(obj, null)
);
}
// if obj type does not match KeyValuePair throw exception
throw new ArgumentException($"obj argument must be of type KeyValuePair<{typeof(TKey).FullName},{typeof(TValue).FullName}>");
}
and usage would be like:
KeyValuePair<string,long> kvp = obj.ToKeyValuePair<string,long>();
I use this simple method:
public Dictionary<string, string> objToDict(XYZ.ObjectCollection objs) {
var dict = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> each in objs){
dict.Add(each.Key, each.Value);
}
return dict;
}
You can use this:
Dictionary<object,object> mydic = ((IEnumerable)obj).Cast<object>().ToList().ToDictionary(px => px.GetType().GetProperty("Key").GetValue(px), pv => pv.GetType().GetProperty("Value").GetValue(pv));
string BaseUrl = "http://www.example.com";
HttpClient client = new HttpClient { BaseAddress = new Uri(BaseUrl) };
PropertyInfo[] properties = object.GetType().GetProperties();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (PropertyInfo property in properties)
{
dictionary.Add(property.Name, property.GetValue(model, null).ToString());
}
foreach (string key in dictionary.Keys)
{
client.DefaultRequestHeaders.Add(key, dictionary[key]);
}
As I understand it, you're not sure what the keys and values are, but you want to convert them into strings?
Maybe this can work:
public static void MyMethod(object obj)
{
var iDict = obj as IDictionary;
if (iDict != null)
{
var dictStrStr = iDict.Cast<DictionaryEntry>()
.ToDictionary(de => de.Key.ToString(), de => de.Value.ToString());
// use your dictStrStr
}
else
{
// My object is not an IDictionary
}
}
object parsedData = se.Deserialize(reader);
System.Collections.IEnumerable stksEnum = parsedData as System.Collections.IEnumerable;
then will be able to enumerate it!
Simple way:
public IDictionary<T, V> toDictionary<T, V>(Object objAttached)
{
var dicCurrent = new Dictionary<T, V>();
foreach (DictionaryEntry dicData in (objAttached as IDictionary))
{
dicCurrent.Add((T)dicData.Key, (V)dicData.Value);
}
return dicCurrent;
}

Categories

Resources