Access Attributes of object inside a dictionary - c#

I am working on a winforms application using C#. I have a dictionary with specific objects, the object have attributes Id and DocType. How can i access the attributes of every object in a foreach statement. i Am trying with the following code but is not working. Any help pls?
foreach (var doc in crs.DocDictionary)
{
Console.WriteLine( doc.Id);
Console.WriteLine(doc.docType);
}

If you foreach on a dictionary you get a sequence of KeyValuePair<TKey,TValue>; try:
foreach (var doc in crs.DocDictionary.Values)
{
Console.WriteLine(doc.Id);
Console.WriteLine(doc.docType);
}
or:
foreach (var pair in crs.DocDictionary)
{
Console.WriteLine(pair.Key);
Console.WriteLine(pair.Value.Id);
Console.WriteLine(pair.Value.docType);
}

Related

Get properties of properties of a class

I want to get the properties of the properties of a class.
What I have right now:
foreach (var v in test.GetType().GetProperties())
{
foreach (var p in v.GetType().GetProperties())
{
}
}
The first foreach loop works fine and gets the properties of the class variable test. However, in the second loop, I get output such as MemberType, ReflectedType, Module etc.. not actual properties.
My goal is to get the properties of the properties of a class and then edit their value (truncate them using another function).
Thanks.
On the second loop GetType() returns a PropertyInfo object. You have to get the propertyType of v as v.PropertyType.GetProperties() to achieve what you want.
So, the code should be:
foreach (var v in test.GetType().GetProperties())
{
foreach (var p in v.PropertyType.GetProperties())
{
// Stuff
}
}
The type returned by v.GetType() is that of PropertyInfo, because v is a property info. You don't want the properties of the PropertyInfo type, you want the properties of the type itself.
Use v.PropertyType, not v.GetType().
GetProperties() gets you PropertyInfo objects which tell you information about the properties of the object. You need to use GetValue to actually get the values of those properties. From there you can repeat the process to get the values of that object's properties.
foreach (var v in test.GetType().GetProperties())
{
var propertyValue = v.GetValue(test);
foreach (var p in propertyValue.GetType().GetProperties())
{
var subPropertyValue = p.GetValue(propertyValue);
Console.WriteLine("{0} = {1}", p.Name, subPropertyValue);
}
}
After editing the value use SetValue to persist it back to the object.

Simplify loop to get the attributes of a class

I have this two foreach loops to get all the attributes, linearly, of one my class.
foreach (PropertyInfo property in GetType().GetProperties())
{
foreach (Attribute attribute in property.GetCustomAttributes(true))
{
}
}
How I can simplify this two loops to one loop or to linq operation to get the class attributes ?
You can rely on SelectMany()
var attributes = GetType().GetProperties()
.SelectMany(p => p.GetCustomAttributes(true));
foreach (var attribute in attributes)
{
// Code goes here
}
Or using query notation:
var attributes=from p in yourObject.GetType().GetProperties()
from a in p.GetCustomAttributes(true)
select a;

Retrieving hierarchy using ADOMD and not MDX query

I want to retrieve hierarchy from one of the cube. I want to form a JSON structure so I am hoping if I can use ADOMD and use a recursive function to get this information and show the result in TreePanel.
I need to form JSON from the output.
foreach (var att in dimension.Hierarchies)
{
foreach (var m in att.Levels[1].GetMembers())
{
var path = att.UniqueName;
}
}
The above code only gets me level 1 attributes. I don't know how to get all the child attributes for given attribute.
Please help
To modify your original code to loop all the levels (instead of just level 1) is simple, but I'm guessing you are after the names of the members within each level.
Your original line var path = att.UniqueName; will return the same value many times over, won't it?
foreach (var att in dimension.Hierarchies)
{
foreach (var lev in att.Levels) //NEW LOOP
{
foreach (var m in lev.GetMembers())
{
var membername = m.UniqueName; //GET VALUE HERE
}
}
}
Where I have used UniqueName you could use any member property - read about ADOMD to find out what is available.

Removing values from a dictionary using loop

I have procedure where I want to remove values from a dictionary based on a certain condition(comparing the key values to another dictionary)
In the foreach loop I am however not allowed to modify the dictionary.
Is there perhaps a better way of doing this?
The key values are string,
foreach (var archive in dictArchivedTitles)
{
foreach (var kvp in dictAllTheFiles)
{
if (kvp.Key == archive.Key)
{
dictAllTheFiles.Remove(kvp.Key);
}
}
}
You can do the following.
foreach (var archive in dictArchivedTitles)
{
if(dictAllTheFiles.ContainsKey(archive.Key)
dictAllTheFiles.Remove(archive.Key);
}
Change
foreach (var kvp in dictAllTheFiles)
to
foreach (var kvp in dictAllTheFiles.ToList())
That way you would work on a copy of dictAllTheFiles
If removing the key of already archived titles is all you want in these nested loops, this would be more efficient and readable (imho):
dictAllTheFiles = dictAllTheFiles.Keys.Except(dictArchivedTitles.Keys)
.ToDictionary(key => key);
Note that you need to addd using System.Linq;
If you do not want to modify either dictionary and have access to LINQ, you could do this:
var nonMatchingEntries =
dictAllTheFiles.Where(kv => !dictArchivedTitles.ContainsKey(kv.Key));

Accessing Linq to XML query results with [] in C#

In this post, the Linq to XML query result are accessed with iterator as follows.
foreach (var elem in elems) {
var res = elem.Elements("ClassKeyName");
foreach (var e in res) {
Console.WriteLine(e.Value);
}
}
Can I access the result with []? For example, I want to use as follows,
foreach (var elem in elems) {
var res = elem.Elements("ClassKeyName");
Console.WriteLine(res[0].Value);
}
However, I got this error message
xmlparse.cs(18,34): error CS0021:
Cannot apply indexing with [] to an expression of type
`System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>'
You'd just have to convert the results to an indexable type, such as a list:
foreach (var elem in elems) {
List<XElement> res = elem.Elements("ClassKeyName").ToList();
Console.WriteLine(res[0].Value);
}
(You can still use var if you want - I've just given it an explicit type to make it clearer in this case.)
If you only need the first, you can res.First().Value. If you need the n-th element res.Skip(n - 1).Value (so the first element is res.Skip(0).Value, the second res.Skip(1).Value...).
The big question is WHY? What do you want to do?

Categories

Resources