1

Is there a way to build on to the select clause of a previously defined LINQ query?

For example:

var stuffQuery =
    from stuff in MyStuff
    select new {
        stuff.Name
    };
var query2 =
    from stuff in stuffQuery
    join otherStuff in YourStuff on stuff.Name equals otherStuff.Name
    select new {
        stuff.*, // how can I accomplish this?
        YourStuff = new {
            otherStuff.PropertyX
        }
    };

The result I want is an object like:

string Name
anonymous YourStuff
        string PropertyX

I thought about using a "Combine" method which would reflectively combine my two anonymous objects into a dynamic. But Linq-to-Sql won't know what to do with the method.

Instead, I think I need a method which returns the select expression. Its parameters would be the first Queryable, and the select expression for the second Queryable. Something like:

var query2 =
    from stuff in stuffQuery
    join otherStuff in YourStuff on stuff.Name equals otherStuff.Name
    GetCombinedSelectClause(stuffQuery, new {
        YourStuff = new {
            otherStuff.PropertyX
        }
    });

How can I accomplish this? I'm not married to any particular syntax-style. However, I'd prefer not to use a string-based solution (such as System.Linq.Dynamic).

Community
  • 1
  • 1
harley.333
  • 3,696
  • 2
  • 26
  • 31
  • Are you saying you don't want to hard-code the field names in the Select statement? – D Stanley Nov 19 '13 at 16:25
  • I want two separate queries. I want to combine these two queries into one. I want the contents of the first select clause to sit side-by-side with the contents of the second select clause. – harley.333 Nov 19 '13 at 16:33

2 Answers2

0

How about ExpandoObject? I believe it can do exactly what you need.

Vitaliy Kalinin
  • 1,791
  • 12
  • 20
0

My question probably isn't worded the best it could be. However, I do not think LINQ queries were designed to accomplish the goal I was after.

I'm closing the question because I simply do not think the answer is achievable.

harley.333
  • 3,696
  • 2
  • 26
  • 31