score:1
Accepted answer
class program
{
class myentity
{
public int id { get; set; }
public string name { get; set; }
public myentity(int id, string name)
{
id = id; name = name;
}
}
static list<expandoobject> join<tsource, tdest>(list<list<tsource>> items, expression<func<tsource, int>> srcaccessor, expression<func<expandoobject, int>> intermediaryaccessor, expression<func<tsource, tsource, expandoobject>> outerresultselector)
{
var joinlambdatype = typeof(expandoobject);
expression<func<expandoobject, tsource, expandoobject>> innerresultselector = (expando, item) => expando.addvalue(item);
var joinmethod = typeof(enumerable).getmethods().where(m => m.name == "join").first().makegenericmethod(typeof(tsource), typeof(tsource), typeof(int), joinlambdatype);
var tolistmethod = typeof(enumerable).getmethods().where(m => m.name == "tolist").first().makegenericmethod(typeof(tdest));
var joincall = expression.call(joinmethod,
expression.constant(items[0]),
expression.constant(items[1]),
srcaccessor,
srcaccessor,
outerresultselector);
joinmethod = typeof(enumerable).getmethods().where(m => m.name == "join").first().makegenericmethod(typeof(tdest), typeof(tsource), typeof(int), joinlambdatype); // from now on we'll be joining expandoobject with myentity
for (int i = 2; i < items.count; i++) // skip the first two
{
joincall =
expression.call(joinmethod,
joincall,
expression.constant(items[i]),
intermediaryaccessor,
srcaccessor,
innerresultselector);
}
var lambda = expression.lambda<func<list<expandoobject>>>(expression.call(tolistmethod, joincall));
return lambda.compile()();
}
static void main()
{
list<list<myentity>> items = new list<list<myentity>> {
new list<myentity> {new myentity(1,"test1_1"), new myentity(2,"test1_2")},
new list<myentity> {new myentity(1,"test2_1"), new myentity(2,"test2_2")},
new list<myentity> {new myentity(1,"test3_1"), new myentity(2,"test3_2")},
new list<myentity> {new myentity(1,"test4_1"), new myentity(2,"test4_2")}
};
expression<func<myentity, myentity, expandoobject>> outerresultselector = (i, j) => new expandoobject().newobject(i, j); // we create a new expandoobject and populate it with first two items we join
expression<func<expandoobject, int>> intermediaryaccessor = (expando) => ((myentity)((idictionary<string, object>)expando)["i0"]).id; // you could probably get rid of hardcoding this by, say, examining the first key in the dictionary
dynamic cc = join<myentity, expandoobject>(items, i => i.id, intermediaryaccessor, outerresultselector);
var test1_1 = cc[0].i1;
var test1_2 = cc[0].i2;
var test2_1 = cc[1].i1;
var test2_2 = cc[1].i2;
}
}
Source: stackoverflow.com
Related Query
- Linq Query multiples tables join with array of varying table names
- 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?
- linq query to join two tables and get the count from one table values from the other
- LINQ Query To Join Two Tables and Select Most Recent Records from Table B corresponding to Table A
- How to use Linq to join entity tables with a cross-reference table
- Linq - Join resultset of 1 query with another table
- Left Join 2 tables with main table using LINQ
- Linq how to join tables with a where clause on each table and a count
- Linq query to Join tables if value not in first table fetch from second
- Linq query to join two tables and return object from first table - PagedList used
- Trouble generating c# linq to entities (EF) query for a 3 table join with or and one table with no key relationships
- How to query a table column into an array with LINQ
- Linq query to select data from table and join 2 tables
- LINQ to Entities query for count and group by with join on 2 tables
- Writing left join in linq query with 5 table
- Linq query with left join having multiple tables
- how to create a left join query for a many to many related table with LINQ
- SQL to LINQ - multiple tables left outer join with where clause referring right table
- How to pull one column from second table in Linq query with join
- How to join a table to two other tables with linq in a complex query?
- Making 1 table join on 2 other tables with LINQ
- How can I use this Linq Query using it with a multiple parameter by Join tables
- Linq Query with three tables join to send alert after x days
- convert SQL query with multiple join on multiple tables using group by on multiple columns and with aggregate function into LINQ
- Asp.netMvc join two tables with linq query
- C# WPF Linq Join 4 tables and Where with varying parameters
- Join Parent, Child and GrandChild tables with latest record from grandchild table using LinQ in C#
- Self Join or Inner Query in a table with Linq to SQL
- How to join three tables in linq with child table sum
- Dynamic Linq query for join two tables with more than one condition(dynamically)
More Query from same tag
- Listing a field from all the entities in a collection
- Edit and save element to XML file using Linq
- Linq Passing table as an object to Table<t>
- LINQ query with multiple joins and contains is not working
- select all attributes and its value for a given node-Linq to xml
- Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON
- ProjectTo does not recognize parameterless constructor
- The query contains references to items defined on a different data context
- VB.NET LINQ To DataTable Select with Where Clause
- Error in form collection MVC 4
- Enumerable.Range() iteration index
- linq how can I fill a dictionary object
- foreach on IEnumerable or IList
- Text manipulation
- How to implement expressions in Generic Repository?
- EF4: Get EntitySet specified as string
- Getting last x consecutive items with LINQ
- How can I add/sum all of the database column according to inputted employeeid and startdate to enddate in ASP MVC?
- changing sql JOIN Syntax to LINQ
- Return only certain columns to JSON in LINQ
- How to return a subquery in LINQ/EF
- Check if results from LINQ query contains a value
- How can I sort by multiple fields in LINQ?
- Convert string response from webclient into array/list
- The expression 'y.Cases' is invalid inside an 'Include' operation
- Informix to linq/sql
- SQLite and LINQ: 'Member access failed to compile expression' (WHERE Condition)
- Can extension methods have generic parameters?
- how to retrieve data between two dates in linq
- OrderBy on List with DynamicProxies