title: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types
body: <p>I need some advice on how to fix the following error: "Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types."</p>
  
  <p>I have a gridview (RadGrid, but that's not important) which is binded on load with data and everything works fine. Data is being binded with the following code:</p>
  
  <pre><code>var ticketList = (from t in db.Ticket
                            select t).ToList();
  
          int idKontakt = Convert.ToInt32(Session["authenticatedUI"]);
  
          Kontakt kontakt = new Kontakt();
          kontakt = db.Kontakt.SingleOrDefault(k =&gt; k.idKontakt == idKontakt);
  
          gvTicketi.DataSource = from t in ticketList
                                 where t.idKontakt == idKontakt
                                 orderby t.idTicket, t.RedniBroj, t.DatumPrijave
                                 select new
                                 {
                                         t.idTicket,
                                         t.idFirma,
                                         t.idKontakt,
                                         t.idManager,
                                         t.idNadredeniTicket,
                                         TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj,
                                         t.Biljeske,
                                         t.DatumDo,
                                         t.DatumPrijave,
                                         t.OpciPrioritet,
                                         t.Opis,
                                         t.OpisZatvoren,
                                         t.Prioritet,
                                         t.Status,
                                         t.Tip,
                                         t.VrstaPrijave,
                                         t.Zatvoren,
                                         t.DatumZatvaranja,
                                         t.IzdanRacun,
                                         NazivKontakta = t.Kontakt == null ? "Bez kontakta" : t.Kontakt.Ime + " " + t.Kontakt.Prezime,
                                         NazivTvrtke = t.Firma.Naziv
                                     };
  </code></pre>
  
  <p>Everything works fine on page load, but when I try to filter datasource problem occurs.</p>
  
  <p>This is the code I use for filtering gridview:</p>
  
  <pre><code>var ticketList = from t in db.Ticket
                           select t;
  
          if (txtBrojTicketaGlavni.Text != string.Empty)
          {
              int glavniBroj = Convert.ToInt32(txtBrojTicketaGlavni.Text);
              ticketList = ticketList.Where(t =&gt; t.idNadredeniTicket == glavniBroj);
          }
  
  
          int idKontakt = Convert.ToInt32(Session["authenticatedUI"]);
  
          Kontakt kontakt = new Kontakt();
          kontakt = db.Kontakt.SingleOrDefault(k =&gt; k.idKontakt == idKontakt);
  
          ticketList.ToList();
  
          gvTicketi.DataSource = from t in ticketList
                                 orderby t.idTicket, t.RedniBroj, t.DatumPrijave
                                 select new
                                 {
                                     t.idTicket,
                                     t.idFirma,
                                     t.idKontakt,
                                     t.idManager,
                                     t.idNadredeniTicket,
                                     TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj,
                                     t.Biljeske,
                                     t.DatumDo,
                                     t.DatumPrijave,
                                     t.OpciPrioritet,
                                     t.Opis,
                                     t.OpisZatvoren,
                                     t.Prioritet,
                                     t.Status,
                                     t.Tip,
                                     t.DatumZatvaranja,
                                     t.VrstaPrijave,
                                     t.Zatvoren,
                                     t.IzdanRacun,
                                     NazivKontakta = t.Kontakt == null ? "Bez kontakta" : t.Kontakt.Ime + " " + t.Kontakt.Prezime,
                                     NazivTvrtke = t.Firma.Naziv
                                 };
  
          if (kontakt.idOvlasti == 2)
          {
              gvTicketi.MasterTableView.GetColumn("CloseColumn").Visible = false;
          }
  
          gvTicketi.DataBind();
  </code></pre>
  
  <p>I figured out that the problem is in gvTicketi.DataSource, in this line and I'm sure that it happens because I didn't convert ticketList to list. The reason I didn't do it is because then I get the other error ("Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)")</p>
  
  <pre><code>TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj,
  </code></pre>
  
  <p>Any help would be appreciated! Thank you!</p>