how to compare adjacent values in data dictionary [closed] - c#

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
how do i get value(i) - value(i-1) (or compare adjacent ) in a data dictionary? I need to flag the adjecent values which are same in the same dictionary

Dictionaries are unordered. There is no position for the items in it, and as a result, there is no such thing as an "adjacent" item.

Basically, you shouldn't be doing this with a regular dictionary. Adjacency/indexing is not a concept for normal dictionaries. If you really need something, use an OrderedDictionary which is some sort of hybrid array/dictionary.

int? lastValue = null;
int diff;
foreach( KeyValuePair<int, int> kvp in myDictionary )
{
if(lastVaue != null)
diff = kvp.value - lastValue;
lastValue = kvp.value;
}
As stated by other foreach (GetEnumerator()) is loose in a Dictionary
If you want order you should use another collection.
OrderedDictionary
If the keys are ordered I think you could use
myDictionary.OrderBy(x => x.Key)

Related

How do I add the sum of the columns of a 2d List<> in a 1d List<> in C# [closed]

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 1 year ago.
Improve this question
I basically want to go through the 2d List<> and deposit the sum of the items in a column to a 1d List. So far have a double for loop but I cant figure out the logic. Any help with how I can start to contruct it logically would be helpful. Thank you. This is the code I have so far:
So if you have this:
var grid = new List<List<int>> {
new() { 1,2 },
new() { 3,4 }
}
You can do:
grid.Pivot().Select(c => c.Sum());
And you'll get an enumerable that is { 4, 6 } (1+3 and 2+4)
You can get Pivot from fubo's answer here (or choose any one of the other suggested transforms)
If I misunderstood and you wanted {3,5} just skip the Pivot part

Remove items from List<T> where condition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I got a List<Person> listA -> where Person has
string name
int age
List<string> jobs
I need to remove from listA all items that jobs.count() < 1, using linq, any ideas?
You shouldn't mutate a list using LINQ, because it's designed for querying, however, List<T> already has a RemoveAll method as part of it's protocol:
listA.RemoveAll(person => person.Jobs.Count < 1);
RemoveAll accepts a Predicate<T> delegate which should return true when an item is to be removed.
However, if you're intention is to create a new list that only contains people with less than one job, you can use LINQ:
var newList = listA
.Where(person => person.Jobs.Count < 1)
.ToList();

Difference between List<int> and Dictionary<int, int>? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why would anyone ever use a dictionary with two integers?
It seems to me like this would just create an indexed collection of integers, where the "key" is the integer index and the "value" is the integer.
Or, in other words, it would be the same as a list of integers.
Now, I suppose having a dictionary allows you to set custom integer keys, so for example you could have pairs like (1,2) (7,3), etc. but this still doesn't make much sense. But when would it actually be practical/useful to use a dictionary as opposed to List?
For example, if you have a database-table with customers.
And you have another table with orders.
You want to get the number of different orders of every customer from the database.
Then you could save it as a Dictionary<int, int>, where the first int is the customer's id and the second int is the number of different orders they have.
The TKey (on a Dictionary<TKey, TValue>) is not the index. It is a key value, you do not need to follow a specifique order in the TKey. The TKey is just a value you use to get an excatly item on the dictionary. To access an item of the dictionary by index, you can get the index by keys collections. For sample:
int key = dictionary.Keys.ElementAt(2); // the 3'rd element
var value = dictionary[key]; // the the value by the key
A List<T> is a list of value and you can access by the index.
var value = list[2]; // get the 3'rd element

Dictionary to anonymous object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a dictionary collection, string, int. I want convert this dictionary to anonymous object with properties that are collection keys and anonymous object propertie's values are dictionary values. Is it a way to do it?
Thank you
Are you looking for this?
var keyVals = dict.Select(kv => new { Key = kv.Key, Value = kv.Value });
But i can't imagine a use case for this. Why do you prefer the anonymous type over the already available dictionary?
Like this :
from x in mydictionairy select new { anonymousKey = x.Key, anonymousValue = x.Value}

implementing a Dictionary<int, Dictionary<int, int>>, order of the keys? [closed]

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 have the choice of where to put both key;
Dictionary<key1, Dictionary<key2, int>>
Dictionary<key2, Dictionary<key1, int>>
if key1 is 32x smaller than key2
which one should I implement to get maximum speed?
does it matter even matter?
is there a better way to implement that?
Probably you get the best performance by implementing a composite key:
struct Key { key1; key2; }
Implement Equals and GetHashCode for it (better yet, the IEquatable<Key> interface).
With this pattern you only need one dictionary and one hash lookup.
so after playing around i went with this solution;
int[,][] myDict;
instead of
Dictionary<int, Dictionary<int, int>> myDict;
or a tuple or struct for both keys

Categories