score:2
this is what you are looking to do:
- merge the two collections of dictionaries.
- group items by the "id" key-value.
- for each group you have multiple dictionaries so use
selectmany
to flatten and thengroupby
on the key. now you can recreate the dictionaries -todictionary
. notice that you might have keys repeating themselves so that is why the nestedgroupby
and for the value select the one you want. here i just usedfirstordefault
so:
var result = firstsourcedata.concat(secondsourcedata)
.groupby(item => item["id"])
.select(group => group.selectmany(item => item)
.groupby(item => item.key)
.todictionary(key => key.key,
value => value.firstordefault().value));
this is the result:
new dictionary<string, object>
{
["id"] = 1,
["sales"] = 58,
["name"] = "some",
["age"] = 30
},
new dictionary<string, object>
{
["id"] = 2,
["sales"] = 58,
["age"] = 30
}
for this test case:
var firstsourcedata = new list<dictionary<string, object>>
{
new dictionary<string, object>
{
["id"] = 1,
["sales"] = 58,
["age"] = 30
},
new dictionary<string, object>
{
["id"] = 2,
["sales"] = 58,
["age"] = 30
}
};
var secondsourcedata = new list<dictionary<string, object>>
{
new dictionary<string, object>
{
["id"] = 1,
["name"] = "some",
["age"] = 30
}
};
score:-1
you can do union:
dictionary<string,object> dict1 = new dictionary<string, object>();
dict1.add("id", 1);
dict1.add("name", "some");
dict1.add("age", 30);
dictionary<string, object> dict2 = new dictionary<string, object>();
dict2.add("id", 1);
dict2.add("sales", 58);
dict2.add("age", 30);
dictionary<string, object> dict3 = new dictionary<string, object>();
dict3 = dict1.union(dict2).todictionary(c => c.key, c => c.value);
resulting values are:
[0] = {[id, 1]}
[1] = {[name, some]}
[2] = {[age, 30]}
[3] = {[sales, 58]}
score:0
you can use join
like this
from dict1 in firstsourcedata
join dict2 in secondsourcedata
on dict1["id"] equals dict2["id"]
select dict1.concat( //concatenates 2 dictionaries together
dict2.where(kv => !dict1.containskey(kv.key)) //chooses non-repeating keys
).todictionary(kv => kv.key, kv => kv.value) //gets a new dictionary from that
if the lengths of the sources are different join
will only select these dictionaries, whose id
s are in the first source. if you want to get all id
s, regardless whether you have data from both sources, you can use defaultifempty
from dict1 in firstsourcedata
join tempdict2 in secondsourcedata
on dict1["id"] equals tempdict2["id"] into joined
from dict2 in joined.defaultifempty(new dictionary<string, object>()) //make a new dictionary if there's none
select dict1.concat(
dict2.where(kv => !dict1.containskey(kv.key))
).todictionary(kv => kv.key, kv => kv.value)
if you want dictionaries from both regardless of id
s refer to this question.
Source: stackoverflow.com
Related Query
- How to Join two dictionary collections linq query
- How to join two table from two different edmx using linq query
- How to join two arrays in a LINQ query and use the result further in the query?
- How can I make a JOIN query with LINQ between two tables (entities) with many-to-many relationship, while there is no joining table in EF?
- How to concatenate two collections by index in LINQ
- How do I most elegantly express left join with aggregate SQL as LINQ query
- linq query to join two tables and get the count from one table values from the other
- How can I combine this code into one or two LINQ queries?
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to do Join in Linq query on column A or column B
- LINQ query over a list of Bool+String pairs. How to join strings without foreach?
- how to join two arrays with linq
- How to use LINQ to combine two or more collections into one collection
- How can I code an outer join using LINQ and EF6?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- How to join two List<string> in c# using LINQ
- How to union two LINQ queries but the second query need have more fields
- How to get SQL query into LINQ form in C# code
- How to add left outer join to grouped and summed LINQ query
- How do I get this Linq query with custom join into a DataSet?
- How linq to nhibernate by join and Separate where in query
- How make LINQ query with GroupBy and LEFT JOIN
- How can I code a Linq query to do an upward Include?
- LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A
- How and I introduce an "or" operator into a linq query join
- How to group the linq query by two id's
- How to join EF query with dictionary
- How to Compare two IEnumerable Collections using LINQ
- How to join Enum with LINQ Query
More Query from same tag
- Linq to check if all value from another list exist
- Error in Linq, Count()
- Convert x => array.Contains(x) expression into x=> x == 1 || x ==2
- mongoDB run queries just like as SQL !
- Excluding items using linq
- Making random number in Linq
- Is there an easy way to return the FIRST LinqToXML result?
- LINQ to SQL and N-Tier layering
- Linq - OrderByDescending and Take(1), will it return max item?
- List from aggregated duplicate objects where a string property concats the strings from the duplicate objects in C#
- Linq Nested Inner Joins
- Convert SortedList to IOrderedEnumerable
- Noob in linq, selecting the first record of each record with the same value
- Linq to entity Check if date of birth is between given set of date ranges(EF6)
- How can I filter the elements in the list by certain criteria and then change their properties with Linq?
- Show searched value in exception when nothing is found with First()
- How to get the Role's Names for each user in a list?
- Lambda expression where column is equal to list items
- InvalidCastException: ConstantExpression to LambdaExpression in MongoDB Driver
- Cannot implicitly convert type 'System.Linq.IQueryable
- string to byte arrays (using linq or regex)?
- Filter Sub-Collection of Collection
- SQL Server CLR Trigger with SMO or Linq?
- C# interpret my query as inner join instead of left outer join on what cases this occurs?
- C# Linq get values from column B if column A matches
- Refactor nested loops into a single LINQ Query
- Cast a LINQ-to-Entites model with a join
- Table aggregation using linq (calculate Average value)
- Select the max,min of nested lists vb.net
- Access child token in JSON