3

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."

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:

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

Everything works fine on page load, but when I try to filter datasource problem occurs.

This is the code I use for filtering gridview:

var ticketList = from t in db.Ticket
                         select t;

        if (txtBrojTicketaGlavni.Text != string.Empty)
        {
            int glavniBroj = Convert.ToInt32(txtBrojTicketaGlavni.Text);
            ticketList = ticketList.Where(t => t.idNadredeniTicket == glavniBroj);
        }


        int idKontakt = Convert.ToInt32(Session["authenticatedUI"]);

        Kontakt kontakt = new Kontakt();
        kontakt = db.Kontakt.SingleOrDefault(k => 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();

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?)")

TicketNumber = t.idNadredeniTicket + "-" + t.RedniBroj,

Any help would be appreciated! Thank you!

wegelagerer
  • 3,600
  • 11
  • 40
  • 60
  • 1
    Try `TicketNumber = t.idNadredeniTicket.ToString() + "-" + t.RedniBroj.ToString(),` – Ankur Oct 24 '11 at 10:40
  • I gotthis error after your line of code:" LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression." – wegelagerer Oct 24 '11 at 10:45
  • What about bringing the 2 things in separate properties in the anonymous object and then in Data grid using Eval method to display the combined string of the 2 properties – Ankur Oct 24 '11 at 10:49
  • @Ankur I'm not sure what do you mean. Do you know any examples of something similar? – wegelagerer Oct 24 '11 at 11:03

1 Answers1

4

The type of ticketList is IQueryable<...>, meaning it is a client side representation of the query.

This line:

ticketList.ToList()

executes the query on the database server, but the results are not stored.

This line:

gvTicketi.DataSource = from t in ticketList
                       orderby t.idTicket, ...

tries to set your DataSource to another IQueryable.

Try:

// construct the query ( query is IQueryable<> )
var query = from t in ticketList
            orderby t.idTicket, ...

// execute on db server ( results is List<> )
var results = query.ToList();

// set DataSource
gvTicketi.DataSource = results;
Nick Butler
  • 24,045
  • 4
  • 49
  • 70