简体   繁体   中英

How to join two or more tables in entity framework and display selected columns from each tables in data grid view

I have generated Entity from database using VS . Now when I want to join two tables from my database and show the result using datagrid view.

my LINQ is :

 result = (from lang in my_entity.LANGUAGE
                              join c in my_entity.COUNTRY
                              on lang.COUNTRY_ID equals c.ID
                              select lang).ToList<LANGUAGE>();

dgvresult.DataSource  =  result;
dgvresult.Refresh();

All the columns from Language tables are shown but none are shown from Country table. I want few columns from language and few columns from country table to be shown in datagrid view.

How do I do it. Any learning links are also appreciated. Also I want to learn DataGrid View in detail. If any one can suggest good books or materials , please do.

You can create a new anonymous type and then bind to it, like so:

 result = (from lang in my_entity.LANGUAGE
                              join c in my_entity.COUNTRY
                              on lang.COUNTRY_ID equals c.ID
                              select new 
                              { 
                                   lang.Col1,
                                   land.col2,
                                   c.Col1,
                                   c.Col2
                              }).ToList();

dgvresult.DataSource  =  result;
dgvresult.Refresh();

Or you can create a view model and simply select the values into it:

public class LangCountryModel 
{
    public string LangCol1 { get; set; }
    public string LangCol2 { get; set; }
    public string CountryCol1 { get; set; }
    public string CountryCol2 { get; set; }
}

     result = (from lang in my_entity.LANGUAGE
                                  join c in my_entity.COUNTRY
                                  on lang.COUNTRY_ID equals c.ID
                                  select new LangCountryModel 
                                  { 
                                       LangCol1 = lang.Col1,
                                       LangCol2 = land.col2,
                                       CountryCol1 = c.Col1,
                                       CountryCol2 = c.Col2
                                  }).ToList();
var result = (from ob1 in lista1   
              join ob2 in lista2    
              on ob1.Id equals ob2.Id  
              select new   
                   {  
                        ob1.Age,  
                        ob2.CarName  
                   }).ToList();

dgvresult.DataSource  =  result;  
dgvresult.Refresh();

Like @pingoo answer with a small edit:

You can do it without using the Join LINQ keyword by using the navigation property COUNTRY of the source table LANGUAGE like this:

result = (from lang in my_entity.LANGUAGE
                select new {
                          lang.Col1,
                          lang.col2,
                          lang.COUNTRY.COUNTRY_ID
                          }).ToList();
dgvresult.DataSource = result; dgvresult.Refresh();`
(...).ToList<LANGUAGE>()
.Select(p=>new{p.LanguageProp,p.COUNTRY.COUNTRYProp,...})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM