Question

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 :)

Was it helpful?

Solution

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
        }
}

OTHER TIPS

Just to expand a little bit on Oded's comment. You can create a new Anonymous type in the select statement by doing:

(x, s) => new { x,s }

You can also specify the names of the items in the anonymous type by doing:

(x, s) => new { TableName=x,TableSchool=s }

If you need to use this outside of the scope of the method however, you need to use a new class such as aquaraga has suggested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top