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

  • Home
  • SEARCH
  • 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 6071177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:01:05+00:00 2026-05-23T10:01:05+00:00

Question for Linq users out there: I’m getting an InvalidCastexception: Specified cast is not

  • 0

Question for Linq users out there: I’m getting an InvalidCastexception: Specified cast is not valid whenever I try to obtain an IEnumerable from revs after making the Linq query. There database is populated and it should be returning values.

Specifically, the error is occurring on the line List<PDP> rev = revs.ToList<PDP>();

Any ideas what’s going on?

short ret;
using (DataContext db = new DataContext())
{
    var play = from p in db.PDP
        where p.ID == id
        select p;
    var revs = play.OrderByDescending(p => p.revision);
    List<PDP> rev = revs.ToList();
    var revNum = revs.ToList().Count() > 0 ? rev.First().revision : 0;
    ret = (short)revNum;
}

EDIT
I’ve clarified some parts of the code.

EDIT 2
rev exists as a debugging variable to narrow down where the error was.

The original code was:

short ret;
using (GasForecastDataContext db = new GasForecastDataContext())
{
    var play = from p in db.PDP
        where p.Play_ID == play_id
        select p;
    var revs = play.OrderByDescending(p => p.revision);
    var revNum = revs.Count > 0 ? rev.First().revision : 0;
    ret = (short)revNum;
}
  • 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-23T10:01:05+00:00Added an answer on May 23, 2026 at 10:01 am

    Is there a specific reason you’re declaring the “rev” list variable at all? “Count()” and “First()” are available on IEnumerable interface ( “revs” ).

    short ret;
    
    using (DataContext db = new DataContext())
    {
        var play = from p in db.PDP
            where p.ID == id
            select p;
    
        var revs = play.OrderByDescending(p => p.revision);
    
        ret = (short) revs.Count() > 0 ? revs.First().revision : 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted a similar question on how scalable linq is. There were so many
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
From a previous question , I learn that Linq to SQL is not able
I recently asked a question about tracing Linq-to-Entities I think that one of the
I am learning LINQ and have a very simple question that I think will
I got one big question. I got a linq query to put it simply
More than about LINQ to [insert your favorite provider here], this question is about
In a related question , my team is about to (hopefully) start using LINQ,
There are a few questions on SO already regarding LINQ pivots and while a
I was just reading a recent question on using conditionals in Linq and it

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.