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

c# entity-framework

Question

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.

1
2
4/8/2014 10:45:40 AM

Accepted Answer

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();
3
4/8/2014 10:59:54 AM

Popular Answer

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();`


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow