Order when iterating over an dictionary [duplicate] - c#

This question already has answers here:
The order of elements in Dictionary
(6 answers)
Closed 8 years ago.
When iterating over a dictionary in C#, are we guaranteed that the order will be same every time we iterate this dictionary?
foreach (var key in myDict.Keys)
{
...
}

According to documentation, no, there is no guarantee:
The order of the keys in the Dictionary<TKey, TValue>.KeyCollection is unspecified,but it is the same order as the associated values in the Dictionary<TKey, TValue>.ValueCollection returned by the Values property.

Related

Force all values in a dictionary to be unique? [duplicate]

This question already has answers here:
Getting multiple keys of specified value of a generic Dictionary?
(16 answers)
C# dictionary type with unique keys and values
(5 answers)
Closed 6 years ago.
I need to maintain a mapping between enum values, and a multi-value type (name+number). e.g.
ABC : {"john", 123}
DEF : {"paul", 234}
GHI : {"ringo", 345}
I planned to do this using a Dictionary of some type but strictly speaking not only the keys should be unique, but the values too - it's like a bi-directional dictionary.
Then I need to be able to write a method MyEnum lookup(string name, int number) which I'd planned would call .Single(x => x.Value.name == name && x.Value.number == number) But I'm struggling to find the right types and apparently the KeyValuePair returned cannot be null which is a problem if there is no match.
What is a good way to approach this?

Check a list has set of values [duplicate]

This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.
How about
list1.Except(list2).Any();
Try using
[listName].Except(SecondListName).Any();
This should work.

How to randomize collection of EF models [duplicate]

This question already has answers here:
Linq to Entities, random order
(12 answers)
Randomize a List<T>
(28 answers)
Closed 9 years ago.
I have a User object that has a collection of Items.
How can I randomize the Items so they dont' appear in the exact same order everytime.
I'm currently ordering by SortOrder (integer), but this will obviously be in the same order everytime.
#foreach(UserItems ui in Model.User.Items.OrderBy(x => x.SortOrder))
{
}
Here's a little trick:
#foreach(UserItems ui in Model.User.Items.OrderBy(x => Guid.NewGuid()))
{
}

Performance IEnumerable<T>.ToList().ForEach() over loop [duplicate]

This question already has answers here:
foreach vs someList.ForEach(){}
(13 answers)
Closed 9 years ago.
Is there any performance difference between these two statements?
IEnumerable<T>.ToList().ForEach(x => Console.WriteLine(x));
and
foreach (var obj in IEnumerable<T>)
Console.WriteLine(obj)
In the first example, you will
Create a list by enumerating the source
Enumerate through the new list and call Console.WriteLine for each element.
In the second example, you will
Enumerate through the source and call Console.WriteLine for each element.
There are two performance penalities to the first over the second:
The creation of the new List object
The double enumeration: over the original source, and then the list

Using a Hashtable to store only keys? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Which collection for storing unique strings?
I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.
Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?
.NET 3.5 includes the HashSet<T> collection type, which sounds like what you want.
HashSet<T>

Categories