views:

189

answers:

1

I have a table with more than 100 columns. I need to join it to another table to pick up one more column. Do I have to list every column in my left table or is there a simpler way.

var query = from p in context.policies
            join s in context.states 
            on p.state_id equals s.state_id
            select new {
              p.column1,
              p.column2,
              p.column3,
              <etc> ...,
              p.column123,
              s.state_name
            };

Is there a way to do this without creating a whole new object just to add one field?

+1  A: 

Instead of listing each field in the anonymous type individually, you could define a field that grabs the entire p object:

... select new { Policies = p, StateName = s.state_name };

You would then have to access the policies fields through obj.Policies.nnn, etc

This is assuming the Policies object can exist outside of the original dataset. If it is a data row or such this might not work.

dthorpe
Awesome....Never knew about this implementation. Thanks :-)
Raja
I didn't know about the explicit naming option on anonymous types until a few weeks ago when I ran into a situation where I was joining two collections and the objects of each collection had the same field names. Needed to use explicit naming to resolve the name conflict/ambiguities.
dthorpe