title: ASP.NET GridView DataSource with LINQ and Joins
body: <p>I'm trying to use LINQ to bind the data in my GridView.</p>
  
  <p>In my Service.cs file I have a method for fetching the entries for a specific logged in user:</p>
  
  <pre><code>public List&lt;tidsregistrering&gt; ShowTidregistreringer()
  {
      var CurrentMedarbejder = FindUser(HttpContext.Current.Session["email"].ToString());
  
      var tempList = (from t in kdc.tidsregistrerings
                      join p in kdc.projekts on t.projektid equals p.id
                      join k in kdc.kundes on t.kundeid equals k.id
                      join o in kdc.øvriges on t.øvrigeid equals o.id
                      where t.medarbejderid == CurrentMedarbejder.id
                      select t).ToList();    
  
      return tempList;
  }
  </code></pre>
  
  <p>Where the kdc is my <strong>DataContext</strong>. I have tried joining the different tables together, but no data is shown in my GridView. If I leave out the joins, I get data... In my other tables I have a column called <code>navn</code> (name). I want that name printet in my GridView instead of the foreign key reference...</p>
  
  <p>My GridView:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="tidsforbrug" HeaderText="Tidsforbrug" /&gt;
          &lt;asp:BoundField DataField="dato" HeaderText="Dato" /&gt;
          &lt;asp:BoundField DataField="&lt;%# Bind("projekt.id") %&gt;" HeaderText="Projekt" /&gt;
          &lt;asp:BoundField DataField="&lt;%# Bind("kunde.id") %&gt;" HeaderText="Kunde" /&gt;
          &lt;asp:BoundField DataField="&lt;%# Bind("øvrige.id") %&gt;" HeaderText="Øvrigt" /&gt;
          &lt;asp:BoundField DataField="done" HeaderText="Done" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView
  </code></pre>
  
  <p>My binding happens at Page_load:</p>
  
  <pre><code>GridView1.DataSource = service.ShowTidregistreringer();
  GridView1.DataBind();
  </code></pre>
  
  <p>How do I do this?</p>
  
  <p><strong>Edit:</strong>
  And for good measure this is my list, as of now... And I want those numbers in projektid, kundeid and øvrigeid to be joined with my foreign key tables.</p>
  
  <p>*Edit2:**
  For even better measure this is how my database tables are created:</p>
  
  <pre><code>CREATE TABLE chef(
      id int identity primary key,
      email varchar(100) unique not null,
      password char(100) not null,
      navn varchar(100) not null
  );
  
  CREATE TABLE medarbejder(
      id int identity primary key,
      email varchar(100) unique not null,
      password char(100) not null,
      navn varchar(100) not null,
      chefid int foreign key references chef(id)
  );
  
  CREATE TABLE projekt(
      id int identity primary key,
      navn varchar(50) not null,
      beskrivelse varchar(255) not null
  );
  
  CREATE TABLE kunde(
      id int identity primary key,
      navn varchar(50) not null,
      beskrivelse varchar(255) not null
  );
  
  CREATE TABLE øvrige(
      id int identity primary key,
      navn varchar(50) not null,
  );
  
  CREATE TABLE tidsregistrering(
      id int identity primary key,
      tidsforbrug float,
      dato datetime,
      projektid int foreign key references projekt(id),
      kundeid int foreign key references kunde(id),
      øvrigeid int foreign key references øvrige(id),
      medarbejderid int foreign key references  medarbejder(id) not null,
      done bit
  );
  </code></pre>
  
  <p><strong>Edit3:</strong>
  I have remade my LINQ-query to this:</p>
  
  <pre><code>List&lt;tidsregistrering&gt; tempList = (from t in kdc.tidsregistrerings
                                     join p in kdc.projekts on t.projektid equals p.id into p_t
                                     join k in kdc.kundes on t.kundeid equals k.id into k_t
                                     join o in kdc.øvriges on t.øvrigeid equals o.id into o_t
                                     from k in k_t.DefaultIfEmpty()
                                     from p in p_t.DefaultIfEmpty()
                                     from o in o_t.DefaultIfEmpty()
                                     where t.medarbejderid == CurrentMedarbejder.id
                                     select t).ToList();
  </code></pre>
  
  <p><strong>But it still doesn't select what's joined...</strong></p>
  
  <p><img src="https://i.stack.imgur.com/eSFQX.png" alt="http://i.stack.imgur.com/eSFQX.png"></p>