Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6740149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:34:17+00:00 2026-05-26T11:34:17+00:00

I need some advice on how to fix the following error: Unable to cast

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:34:18+00:00Added an answer on May 26, 2026 at 11:34 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some advice I am trying to write a validator which only fires when
I need some advice on how to successfully update mutiple rows in my database
I need some advice. I’m building an app that has a sequence of 4
I need some advice regarding which database I should use. I want to create
I need some advice of how to setup my tables I currently have a
I need some advice for a design which I have to implement in application.
I need some advice on which Swing Components to choose in order to achieve
I need some advice on what kind of pattern(s) I should use for pushing/pulling
I need some advice from experts :) I will develop a website using PHP
I need some advice, what is the rule of the thumb when creating Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.