score:1
You can use SelectMany
to expand these kinds of structures:
var expanded = dictionary.SelectMany(outer =>
outer.Value.Select(inner => new {
OuterKey = outer.Key,
InnerKey = inner.Key,
Value =inner.Value
})
);
now you can do:
foreach (var item in expanded)
{
Console.WriteLine("{0},{1},{2}", item.OuterKey, item.InnerKey, item.Value);
}
After that, if you want to extract the inner key / values combination, you can just do:
var byInnerKey = expanded.ToLookup(item=> item.InnerKey, item => item.Value)
which is real easy to print using something like
foreach (var item in byInnerKey)
{
string values = string.Join(", ",item);
Console.WriteLine(item.Key + ", " + values);
}
Note: this uses this overload of the string.Join
method that is only available in .net 4 and above.
For .net 3.5 and below you can either loop the values, with a string
/ StringBuilder
, or cast them out to a strings array explicitly
string values = string.Join(", ",item.Select(d => d.ToString()).ToArray());
score:0
Something is weird about this question.. and it doesn't make sense..
if you are using Dictionary<double, Dictionary<double,double>>
then there would be a
key1, innerdictkey, innerdictvalue
but you are referring to a dict1val1? which is the inner dictionary it self..
in anycase you could loop through the dictionary like this
foreach(var kv in outerdic){
Console.WriteLine(kv.Key + ',' + kv.Value); // kv.Value would be a Dictionary<double, double>
Console.WriteLine(kv.Value[kv.Key]); // this would print the inner value of inner dictionary
}
score:0
If I understand it, the structure looks like this:
dict[1.0] ==> dictionary [1.0] ==> 1.1
[1.1] ==> 1.3
[1.2] ==> 1.5
dict[2.0] ==> dictionary [1.0] ==> 1.2
[1.1] ==> 1.4
[1.2] ==> 1.6
Then we just step through the first dictionary, and match it to the second dictionary.
var dict2 = dict[2.0];
var lstResults = dict[1.0].Select(kvp=> String.Format("{0},{1},{2}",
kvp.Key, kvp.Value, dict2[kvp.Key])
.ToList();
score:1
var d1 = new Dictionary<double, double> { { 1.0, 1.1 } };
var d2 = new Dictionary<double, double> { { 1.0, 1.2 } };
var d3 = new Dictionary<double, double> { { 1.1, 1.3 } };
var d4 = new Dictionary<double, double> { { 1.1, 1.4 } };
var dict1 = new Dictionary<double, Dictionary<double, double>> { { 1.0, d1 }, { 2.0, d3 } };
var dict2 = new Dictionary<double, Dictionary<double, double>>() { { 3.0, d2 }, { 4.0, d4 } };
var keys = dict1.Values.SelectMany(dict => dict.Keys.ToList());
var collection = keys.Select(key1 => new
{
Key = key1,
Values = keys.SelectMany(key =>
dict1.Values.Where(k1 => k1.ContainsKey(key1)).Select(k1 => k1[key1]).Union(
dict2.Values.Where(k2 => k2.ContainsKey(key1)).Select(k2 => k2[key1]))
).Distinct().ToList()
}).ToList();
foreach (var x in collection)
{
Console.Write(x.Key + ": ");
foreach (var y in x.Values)
{
Console.Write(y + ",");
}
Console.WriteLine();
}
Source: stackoverflow.com
Related Query
- Parsing inner double Dictionary using LINQ?
- Using a dictionary as a source of Regex.Replace patterns in a Linq statement
- LINQ double INNER JOIN on query translation when using selectMany
- Convert string[] to int[] in one line of code using LINQ
- Convert list to dictionary using linq and not worrying about duplicates
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- Using Linq to objects, how to create an empty dictionary of <string, string> easily?
- populate a dictionary using linq
- How to select multiple values from a Dictionary using Linq as simple as possible
- Convert dictionary values to list using linq
- How do I convert from a Dictionary to a SortedDictionary using LINQ in C#?
- Creating a dictionary from another dictionary using LINQ
- Processing a C# Dictionary using LINQ
- Convert DataRow to Dictionary using LINQ
- create a dictionary using 2 lists using LINQ
- Converting a resource set into dictionary using linq
- Count values in Dictionary using LINQ and LINQ extensions
- Convert a list to a dictionary and sum up values using linq
- Deserialize JSON to dictionary using LINQ to JSON
- Any better way for converting array to concurrent dictionary using linq or IEnumerable?
- How do you flatten a Linq query after using double grouping?
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Parsing of XML string (with namespace) using LINQ
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- Reversing Dictionary using LINQ in C#
- LINQ Source Code Available
More Query from same tag
- LINQ statement, select field that has specific attribute
- SQL to LINQ - Left Join two conditions
- Many to many linq query Entity Framework
- Linq Lambda join expression to XML
- MongoDB query with FilterDefinitionBuilder and Linq
- LINQ to entities - left join with condition
- ASP.NET MVC - Join Tables using LINQ
- Return all elements except for a specific element that also has no value
- LINQ to objects changing the display column
- Best way to build a linq query based on user selection in VB
- Filter in linq with ID's in a List<int>
- Can I update multiple columns using LINQ?
- How do I minimize C#, LINQ code in Deleting Multiple Records
- IEqualityComparer and Linq Distinct - Hard Code GetHashCode()
- Lazy loading repeater has linq query not yielding results
- .ToList throwing exception
- Equivalent of File.ReadLines(filepath) C# in Javascript
- Merging result of two queries
- Linq - get compare with transliterated value of fields
- Can we control LINQ expression order with Skip(), Take() and OrderBy()
- Return type of Linq on Datatable
- RavenDB - Build a dynamic query with OR filters
- Linq to XML: Get all nodes that contain certain children
- Can I use Func/Predicate or Linq expression to create generic filtering of a List<T>?
- c# LINQ query filling a Tuple List
- What's a clean way to break up a DataTable into chunks of a fixed size with Linq?
- Why am I not able to insert a value to a StringBuilder inside a LINQ Select() query
- CRM 2011 LINQ: Invalid ‘where’ condition. An entity member is invoking an invalid property or method
- Monotouch JIT error on device with LINQ
- Nested Generic Lambdas in LINQ