Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem with the algorithm Fisher in c#.
I have a class system of SystemDecyzyjny. In it I have a dictionary.
public Dictionary<int, Dictionary<int, double>> posortowane = new Dictionary<int, Dictionary<int, double>>();
Data in dictionary:
How to display with posortowane data in Form1.cs in this form:
2 4 1 3
. . . .
. . . .
How to refer to the dictionary and display data from it attributes?
If you want to access the value of the key 0, then you just write this:
posortowane[0]
This returns a Dictionary<int, double>.
Now you can read the value with key 0 of the latter dictionary as:
posortowane[0][0]
The value with the key 1 as:
posortowane[0][1]
and so on and so forth.
It sounds like you want to print out all keys of the inner dictionary. This can be done in the following way:
foreach (var outerKey in posortowane.Keys)
{
foreach (var inner in posortowane[outerKey])
{
Console.WriteLine(inner.Key);
}
}
If you want to print out the both the keys and values of the inner dictionary you could do:
foreach (var outerKey in posortowane.Keys)
{
foreach (var inner in posortowane[outerKey])
{
Console.WriteLine($"{inner.Key}:{inner.Value}");
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
In PHP and Python I can deserialise a JSON document to a dictionary structure. That's the default behaviour and there are not many other options anyway. In C#, hovever, all is typed and System.Text.Json methods want to know what type to deserialise into. Since I don't know the structure of the JSON document and just need to pass it on to something that can handle a dictionary, I need to convert it to that.
This doesn't work as expected:
var dict = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>("{ \"a\": 1, \"b\": { \"c\": 3 } }");
At least what the VSCode debugger shows me, I have a dictionary with the keys "a" and "b" but the values are anything. I'd expect to see an int as value for "a" and another Dictionary as value for "b" with an int for the value "c".
How can this be achieved?
I'm looking for something like this:
// Does not exist:
var dict = System.Text.Json.JsonSerializer.DeserializeToDictionary("{ \"a\": 1, \"b\": { \"c\": 3 } }");
I'm trying to convert a class that I've written in Python because I hit other roadblocks in Python. I'm more experienced in C# so I'd like to solve the problem there, but JSON for dynamic data isn't a strength of C#. Seems I have to write some classes of my application in Python and others in C# to get it done.
Newtonsoft.Json fits much better for the complicated cases. You can try this code
using Newtonsoft.Json;
var jsonParsed = JObject.Parse(json);
var dict = new Dictionary<string, object>();
AddDictionaries(dict,jsonParsed);
public void AddDictionaries(Dictionary<string, object> dict, JObject jsonObj)
{
foreach (var prop in jsonObj)
{
if (prop.Value.Type != JTokenType.Object)
dict.Add(prop.Key, prop.Value);
else
{
var nestedDict = new Dictionary<string, object>();
AddDictionaries(nestedDict, (JObject)prop.Value);
dict.Add(prop.Key, nestedDict);
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I created a list in C# with two object type, and I need to show the object with type ClientPhysique. ClientPhysique and ClientMoral extend the Client class.
public List<Client> Clients = new List<Client>();
#region Methodes
// Ajouter un Client:
public void Ajouter()
{
ClientPhysique CP = new ClientPhysique("EE111111", "Ahmed", "Yassine", 1, 06020202, "Rue Gmasa, Marrakech, 40000");
ClientMorale CM = new ClientMorale("A1414", "Rue mhamid, A6, Marrakech, 40160", 12121212, 1000000, 6, 06060606, "Titwan de Titwan");
Clients.Add(CP);
Clients.Add(CM);
}
You can narrow down your list with the LINQ Method OfType. It allows you to iterate the list items with a specific type, ignoring all other items:
foreach(ClientPhysique item in Clients.OfType<ClientPhysique>()) {
//Do something with your item
}
you could use Oftype as follows
var result = Clients.OfType<ClientPhysique>();
foreach (var element in result)
{
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I created Class Disk
Disk disk1 = new Disk();
Hashtable myCatalog = new Hashtable();
try
{
myCatalog.Add("Disk1", disk1);
}
catch
{
Console.WriteLine("An element with Key = \"Disk1\" already exists.");
}
Disk valueColl = (Disk)myCatalog.Values;
valueColl.
And here I have a problem.
How I can use this method ShowCompositions();
Values is a ICollection containing the values in the Hashtable.
You could do this.
Disk valueColl = (Disk)myCatalog["Disk1"]; // access element with `Key`
valueColl.ShowCompositions(); // this will work
On other note as #jamesthorpe highlighted in comments , try using Dictionary over Hashtable, it has added advantages.
Easiest way
Hashtable hashtable = new Hashtable();
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
var topLevel= resultRows.GroupBy(g => g["CustomerAccountType"]);
I want to select columns only like Actual1, Actual2, Actual3, etc. The names of the columns are not known upfront - they are composed dynamically.
I am not using a strongly type object because resultsRows is a constructed Dictionary from SQL cubes.
I want to dynamically add ActualN...to..ActualN to a collection as dictionary.
You can use Select to make a projection of Actual1, Actual2, Actual3:
var topLevel= resultRows
.Select(d => new Dictionary<string,object> {
{"Actual1", d["Actual1"]}
, {"Actual2", d["Actual2"]}
, {"Actual3", d["Actual3"]}
});
This will remove all columns other than Actual1, Actual2, Actual3 from your dictionary.
I want Actual1 to ActualN added dynamically without knowing how many are there
You can do that, too:
// Construct this list dynamically
var selection = new List<string> {
"Actual1", "Actual2", ..., "ActualN"
};
var topLevel= resultRows.Select(d => selection.ToDictionary(s => s, s => d[s]));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having a return type of a function being set to IEnumerable<Dictionary<string, object>> and I need to get the key, value pairs.
Using the appropriate key, I need to change the value.
I have a solution when the return type is a Dictionary but not when IEnumerable<Dictionary<string, object>>
I finally need to modify the value after, the specific key is found.
These following solutions maybe what your looking for, However you'll have to work out which one is more appropriate based on your circumstances
Given
var myKey = "someKey";
Assuming, your list of dictionaries contains the key your looking for, only once
var aDictionary = MyEnumerable.Single(x => x.ContainsKey(myKey));
aDictionary[myKey] = myNewValue;
An InvalidOperationException will be thrown if
More than one key is found
No keys are found
Assuming your list of dictionaries may or may not contain the key your looking for, only once
var aDictionary = MyEnumerable.SingleOrDefault(x => x.ContainsKey(myKey));
if(aDictionary != null)
{
aDictionary[myKey] = myNewValue;
}
An InvalidOperationException will be thrown if
More than one key is found
Assuming there may be multiple occurrences of your key
foreach (var aDictionary in MyEnumerable.Where(x => x.ContainsKey(myKey)))
{
aDictionary[myKey] = myNewValue;
}
Update
It seems you might be confused with the type IEnumerable<Dictionary<string, object>>
IEnumerable is a list (for the point of this conversation)
A Dictionary represents a collection of keys and values.
So what you have is a list of collections of keys and values
You can iterate through the IEnumerable> using a foreach sentence:
(this is just an example to help to illustrate)
IEnumerable<Dictionary<string, object>> colectionDict;
foreach(var dict in colectionDict) //dict is an object of type Dictionary<string,object>
Also, you can use a variable type Enumerator.
var enum = colectionDict.GetEnumerator();
while(enum.Next){
var dict = enum.Current; // dict is an object of type Dictionary<string,object>
}