lambda inner join and select all

zey picture zey · Jun 1, 2013 · Viewed 12.2k times · Source

Here is how I inner join in lambda ,

var myObject = tableNames.Join(tableSchool,  x => x.sID , s => s.schoolID ,  
( (x,s) => new {   }  ) ).ToList();

I have many fields in both tableNames and tableSchool .
By my lambda query , if I have 10 fields in tableNames , I've to write down all of 10 fields in new { } 10 times.
What I want to know is how can I select all fields of tableName table and one field from tableSchool .

Example

tableName     tableSchool
---------     ------------ 
Nfield1       Sfield1
Nfield2       Sfield2
Nfield3       Sfield3
Nfield4
Nfield5

I want to get all fields from tableName and just one field (Sfield1) from tableShcool . I want to bind this datasource to asp:GridView :)

Answer

aquaraga picture aquaraga · Jun 1, 2013

If you want details of both tables, then you could have:

List<AllDetails> myObject = tableNames.Join(tableSchool, x => x.sID, s => s.schoolID, ((x, s) => new AllDetails(x, s))).ToList();

where:

public class AllDetails
    {
        private TableName tabName;
        private TableSchool tabSchool;

        public AllDetails(TableName tableName, TableSchool tableSchool)
        {
            //Assign fields here
        }
}