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
- C# equivalent of C++ std::string find
- Is it possible to get populate class object from xml?
- get specific properties within the generic list using linq
- “CLR detected an Invalid Program” when compiling a constructor for List<T>
- Many-To-Many Entity Framework Update
- LINQ from a sql join
- (MVC) LINQ to Entities does not recognize the method 'Boolean VerifyHashedPassword(Byte[], System.String)' method
- Clean linq implementation to filter a list by grandchildren
- Why is the "Select" of a Method Syntax is in another parenthesis?
- Custom sort of DataTable where index is needed
- Passing a Func without knowing the object instance
- Where does the LINQ query syntax come from?
- Linq join without equals
- How to add a where clause to this Entity Framework Linq statement
- LINQ lambda to split a recordset on unknown key
- Insert with Linq-to-SQL sometimes fails
- How to keep order by query result as in same order if it given to sub query?
- Check null condition for string date conversion to DateTime inside object
- Linq code refactoring
- How can I count number of objects in a list with a unique property with linq/lambda?
- LINQ Projection AFTER optional parameter filtering
- Where clause with multiple unknown conditions
- .NET - How to effectively search through database for multiple fields in one textbox?
- Trying to convert PIVOT SQL into LINQ lambda exp
- How to get the value by a key from a super nested Json
- Best algorithm for age?
- Extra Enumeration Wrappers .net
- Bind the SQL data to model property in Entity Framework
- Entity Framework OrderBy().Where() expression
- Take Action on Item in List B If That Item Does Not Exist in List A