Using LINQ to SQL Join to Add One Column to a Complex Object

I have a table with over 100 columns. I need to join it in another table to pick up another column. Do I need to list every column in the left table or is there an easier 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?

+2


a source to share


1 answer


Instead of listing each field in the anonymous type separately, you can define a field that captures the entire p object:

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

      



Then you will need to access the policy fields via obj.Policies.nnn, etc.

This assumes that the Policies object may exist outside of the original dataset. If it is a data string or it may not work.

+2


a source







All Articles