I have a list of my class
List<Example> exampleList
Which already has all the data inside of it. I need to create a dictionary
Dictionary<string, List<Example>> exampleDictionary
The Key needs to be Example.Name and the value needs to Example
Here is my code below. The problem is Example.Name can be the same. I need to group by Name. I need to loop through my list and if the Name does not exist add new Key and Value otherwise add the Value to the Key. I know I am setting this up wrong but I can't seem to figure out the correct way of doing this.
foreach(var x in exampleList)
{
if(!exampleDictionary.ContainsKey(x.Name)
exampleDictionary.Add(x.Name, x)
else
exampleDictionary[x.Name] = x;
}
I know this code wouldn't build. I am not sure how to set this up.
You can use LookUp() extension method:
var lookup = exampleList.ToLookUp(e => e.Name);
This method returns a Lookup<string, Example>, a one-to-many dictionary that maps keys to collections of values.
But your code can be fixed grouping by Name and adding each group to exampleDictionary:
foreach (var g in exampleList.GroupBy(e => e.Name))
exampleDictionary.Add(g.Key, g.ToList());
Or
var exampleDictionary = exampleList.GroupBy(e => e.Name).ToDictionary(g => g.Key, g => g.ToList());
This should work
Dictionary<string, List<Example>> exampleDictionary = new Dictionary<string, List<Example>>();
foreach(var x in exampleList)
{
if(!exampleDictionary.ContainsKey(x.Name)) {
exampleDictionary[x.Name] = new List<Example>();
}
exampleDictionary[x.Name].Add(x);
}
You can also use ToDictionary extension method to achieve what you want:
Dictionary<string, List<Example>> exampleDictionary=exampleList.GroupBy(e => e.Name)
.ToDictionary(g => g.Key,g.ToList());
Basically the same as user469104 (+1)
List<Example> le = new List<Example>() { new Example("one"), new Example("one"), new Example("two") };
Dictionary<string, List<Example>> de = new Dictionary<string,List<Example>>();
foreach (Example e in le)
{
if (de.ContainsKey(e.Name))
de[e.Name].Add(e);
else
de.Add(e.Name, new List<Example>() { e });
}
Related
I have this
foreach (KeyValuePair<string, Object> tempData in tempList.ToDictionary(x => x.Key, y => y.Value))
{
tempData["fahrzeugA"] = "s";
}
But using tempData["fahrzeugA"] = "s"; will not work.
I get:
Cannot apply indexing with [] to an expression of type
'System.Collections.Generic.KeyValuePair'
What is the correct syntax if I have an existing key fahrzeugA, which I want to alter ?
You can apply this :
var tempList = new List<Test>();
var dic = tempList.ToDictionary(x => x.Key, y => y.Value);
foreach (var tempData in dic)
{
dic[tempData.Key] = "s";
}
You can't change the key value pair since it is an immutable struct. The only way to change it is to create a new instance. That instance would live independent from the dictionary.
If you want to change the value in the dictionary, use the indexer property on the dictionary to change the value.
And even then, the dictionary will go out of scope immediately, so there is no use setting it. It won't affect the original list.
Check KeyValuePair.Value Property. It's readonly and can't be altered.
ToDictionary creates a new object. You can't alter original object by accessing its elements' value.
You have to remove this specific item from original list and add new item of the same key back.
var removeIndex = tempList.FindIndex(kp => kp.Key == "fahrzeugA");
tempList.RemoveAt(removeIndex);
tempList.Add(new KeyValuePair<string, string>("fahrzeugA", "s"));
If there are multiple "fahrzeugA" items (it's valid in list but not valid in dictionary), use RemoveAll instead.
If your tempList is List<KeyValuePair<string, Object>> type:
for (var i = 0; i < tempList.Count; ++i) {
if (tempList[i].Key == "fahrzeugA") {
tempList[i] = new KeyValuePair<string, object> ("fahrzeugA", "s"); // KeyValuePair<string, object> might be changed with your own type if you use something else.
break; // If you want to modify only first KeyValuePair.
}
}
If you have successfully turned your tempList into a dictionary, there can only be one "fahrzeugA" (since all keys must be unique), so looping makes no sense.
You should be able to just say:
var dictionary = tempList.ToDictionary(x => x.Key, y => y.Value);
dictionary["fahrzeugA"] = "s";
If you don't want to create the dictionary in the first place, you could do this:
var matchingKeyValuePair = tempList.SingleOrDefault(x => x.Key == "fahrzeugA");
if (matchingKeyValuePair != null) matchingKeyValuePair.Value = "s";
If you are using a list of .NET KeyValuePair<TKey, TValue>, which is an immutable struct, you can replace the value with a new KeyValuePair, like this:
var matchingIndex = tempList.FindIndex(x => x.Key == "fahrzeugA");
if (matchingIndex >= 0)
tempList[matchingIndex] = new KeyValuePair<string, string>("fahrzeugA", "s");
Note, this assumes that you only have one item with a key of "fahrzeugA".
I have a snippet as follow. Basically I would like to create a dictionary from the series I currently have. If I did it the long way, there was no problem, the code run fine and I got the expected results.
class Program
{
static void Main(string[] args)
{
List<int> series = new List<int>() { 1,2,3,4 };
Dictionary<int, List<int>> D = new Dictionary<int,List<int>>();
foreach (var item in series)
{
D.Add(item, seriesWithinSeries(item));
}
}
public static List<int> seriesWithinSeries(int seed)
{
return Enumerable.Range(0, seed).Where(x => x < seed).ToList();
}
}
How do I convert that into Linq? I have tried this:
D = series.Select(x=> { return new (x,seriesWithinSeries(x));}).ToDictionary<int,List<int>>();
But the compiler complaints I need a type for the new, which makes sense. However, I do not really know how to fix it. Please help.
ToDictionary doesn't have a parameterless version. You probably want this:
var result = series.ToDictionary(x => x, x => seriesWithingSeries(x));
So anonymous type is not needed here. But for the sake of a complete explanation here is a correct syntax with an anonymous type:
var result = series
.Select(x => new { Key = x, Value = seriesWithinSeries(x) })
.ToDictionary(pair => pair.Key, pair => pair.Value);
I have below code in c# 4.0.
//Dictionary object with Key as string and Value as List of Component type object
Dictionary<String, List<Component>> dic = new Dictionary<String, List<Component>>();
//Here I am trying to do the loping for List<Component>
foreach (List<Component> lstComp in dic.Values.ToList())
{
// Below I am trying to get first component from the lstComp object.
// Can we achieve same thing using LINQ?
// Which one will give more performance as well as good object handling?
Component depCountry = lstComp[0].ComponentValue("Dep");
}
Try:
var firstElement = lstComp.First();
You can also use FirstOrDefault() just in case lstComp does not contain any items.
http://msdn.microsoft.com/en-gb/library/bb340482(v=vs.100).aspx
Edit:
To get the Component Value:
var firstElement = lstComp.First().ComponentValue("Dep");
This would assume there is an element in lstComp. An alternative and safer way would be...
var firstOrDefault = lstComp.FirstOrDefault();
if (firstOrDefault != null)
{
var firstComponentValue = firstOrDefault.ComponentValue("Dep");
}
[0] or .First() will give you the same performance whatever happens.
But your Dictionary could contains IEnumerable<Component> instead of List<Component>, and then you cant use the [] operator. That is where the difference is huge.
So for your example, it doesn't really matters, but for this code, you have no choice to use First():
var dic = new Dictionary<String, IEnumerable<Component>>();
foreach (var components in dic.Values)
{
// you can't use [0] because components is an IEnumerable<Component>
var firstComponent = components.First(); // be aware that it will throw an exception if components is empty.
var depCountry = firstComponent.ComponentValue("Dep");
}
You also can use this:
var firstOrDefault = lstComp.FirstOrDefault();
if(firstOrDefault != null)
{
//doSmth
}
for the linq expression you can use like this :
List<int> list = new List<int>() {1,2,3 };
var result = (from l in list
select l).FirstOrDefault();
for the lambda expression you can use like this
List list = new List() { 1, 2, 3 };
int x = list.FirstOrDefault();
You can do
Component depCountry = lstComp
.Select(x => x.ComponentValue("Dep"))
.FirstOrDefault();
Alternatively if you are wanting this for the entire dictionary of values, you can even tie it back to the key
var newDictionary = dic.Select(x => new
{
Key = x.Key,
Value = x.Value.Select( y =>
{
depCountry = y.ComponentValue("Dep")
}).FirstOrDefault()
}
.Where(x => x.Value != null)
.ToDictionary(x => x.Key, x => x.Value());
This will give you a new dictionary. You can access the values
var myTest = newDictionary[key1].depCountry
Try this to get all the list at first, then your desired element (say the First in your case):
var desiredElementCompoundValueList = new List<YourType>();
dic.Values.ToList().ForEach( elem =>
{
desiredElementCompoundValue.Add(elem.ComponentValue("Dep"));
});
var x = desiredElementCompoundValueList.FirstOrDefault();
To get directly the first element value without a lot of foreach iteration and variable assignment:
var desiredCompoundValue = dic.Values.ToList().Select( elem => elem.CompoundValue("Dep")).FirstOrDefault();
See the difference between the two approaches: in the first one you get the list through a ForEach, then your element. In the second you can get your value in a straight way.
Same result, different computation ;)
There are a bunch of such methods:
.First .FirstOrDefault .Single .SingleOrDefault
Choose which suits you best.
var firstObjectsOfValues = (from d in dic select d.Value[0].ComponentValue("Dep"));
I would to it like this:
//Dictionary object with Key as string and Value as List of Component type object
Dictionary<String, List<Component>> dic = new Dictionary<String, List<Component>>();
//from each element of the dictionary select first component if any
IEnumerable<Component> components = dic.Where(kvp => kvp.Value.Any()).Select(kvp => (kvp.Value.First() as Component).ComponentValue("Dep"));
but only if it is sure that list contains only objects of Component class or children
I want to take a List, and generate a Dictionary which maps each element to its index in the List. I can do this like so, for a List<string>:
var myList = new List<string>{ /* populate list */ };
var orderMap = new Dictionary<string, int>();
foreach (var element in myList)
{
orderMap[element] = myList.IndexOf(element);
}
Basically, I want to take a list like:
Apple
Banana
Orange
And return a map showing indices:
Apple -> 0
Banana -> 1
Orange -> 2
How can I do this with Linq? I think something like this should work:
orderMap = myList.Select( x => /* return a key value pair mapping x to myList.IndexOf(x) */ );
But I can't figure out the right syntax for it. Besides, can you refer to the list itself in the delegate used for Select?
While you can refer to the list within the delegate, it's not generally a good idea. You really want to use the overload of Select which provides the index as well as the value:
var dictionary = list.Select((value, index) => new { value, index })
.ToDictionary(p => p.value, p => p.index);
Note that this will throw an exception if you have any duplicate elements.
You could try the ToDictionary extension method:
int index = 0;
orderMap = myList.ToDictionary(x => x, x => index++);
Take a look at this overload of ToDictionary<TKey, TValue>(). It takes to functions to convert the input element into a Key and a Value.
e.g.
var myList = new List<string>{ /* populate list */ };
var orderMap = myList.ToDictionary(x => myList.IndexOf(x), x => x);
However, one problem with this is if the elements of myList aren't unique.
I'm writing a tool, and the first part of that tool is to collect all the header files in our public API. The problem is, two of the header files have duplicate file names (but they reside in different folders). This will cause a problem when creating a dictionary.
Originally I wrote a foreach loop to collect FileInfo instances into a dictionary. However lately I'm learning LINQ, and I wanted to convert the foreach loop into a LINQ statement.
The problem is when it executed, it complained about the duplicate file name.
Here is the original code:
public Dictionary<String, FileDependency> GetSDKFiles(DirectoryInfo dir)
{
Dictionary<String, FileDependency> list = new Dictionary<String, FileDependency>();
foreach (FileInfo info in dir.EnumerateFiles("*.h", SearchOption.AllDirectories))
{
String key = info.Name.ToLower();
if (list.ContainsKey(key) == false)
{
list.Add(key, new FileDependency(info.FullName));
}
else
{
Debug.Print("Duplicate key: {0}", info.Name);
Debug.Print(" File: {0}", info.FullName);
Debug.Print(" Have: {0}", list[key].FullFileName);
}
}
return list;
}
Which I tried turning into LINQ like so:
public Dictionary<String, FileDependency> GetSDKFilesLINQ(DirectoryInfo dir)
{
var files = from info in dir.EnumerateFiles("*.h", SearchOption.AllDirectories)
let key = info.Name.ToLower()
let dep = new FileDependency(info.FullName)
select new { key, dep };
return files.ToDictionary(v => v.key, v => v.dep);
}
However at runtime I get this:
An item with the same key has already been added.
In the foreach loop it was easy to avoid that, since I called the ContainsKey method to make sure I had no duplicates. But what is the LINQ equivalent?
Do I use where? - How?
Do I use group? - How?
Thanks.
var files = dir.EnumerateFiles("*.h", SearchOption.AllDirectories)
.GroupBy(file => file.Name.ToLower())
.Select(group => new {Key = group.Key, Value = group.First()})
.ToDictionary(a => a.Key, a => new FileDependency (a.Value.FullName));
If you have MoreLinq, you can do:
var files = dir.EnumerateFiles("*.h", SearchOption.AllDirectories)
.DistinctBy(file => file.Name.ToLower())
.ToDictionary(file => new FileDependency (a.Value.FullName));
Alternatively, you can write your own IEqualityComparer implementation for the files and use the standard Distinct method. The whole problem here is that Distinct (at least as of .NET 3.5) doesn't come with an overload that allows for inserting your own definition of "distinctness" as a lambda expression.
You could group by key and take the first value from the group for dep:
public Dictionary<String, FileDependency> GetSDKFilesLINQ(DirectoryInfo dir)
{
var files = from info in dir.EnumerateFiles(
"*.h", SearchOption.AllDirectories)
let key = info.Name.ToLower()
let dep = new FileDependency(info.FullName)
group dep by key into g
select new { key = g.Key, dep = g.First() };
return files.ToDictionary(v => v.key, v => v.dep);
}
That will silently ignore duplicates. Alternately, you could use a Lookup instead of a Dictionary:
public ILookup<String, FileDependency> GetSDKFilesLINQ2(DirectoryInfo dir)
{
var files = from info in dir.EnumerateFiles(
"*.h", SearchOption.AllDirectories)
let key = info.Name.ToLower()
let dep = new FileDependency(info.FullName)
select new { key, dep };
return files.ToLookup(v => v.key, v => v.dep);
}
The indexer on the lookup will return an IEnumerable<FileDependency>, so you can see all the values.