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 6330325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:51:01+00:00 2026-05-24T17:51:01+00:00

I need to run a stored procedure on SQL Server, using LINQ, and throw

  • 0

I need to run a stored procedure on SQL Server, using LINQ, and throw an exception if it returns any rows. The following code works, although the foreach loop does not communicate my intent well enough:

foreach (var badData in context.BadDataForDay(today))
{
    throw new Exception("Bad data found");
}

I wanted to write something like IF EXISTS() in SQL, but with LINQ:

var badData = from p in context.BadDataForDay(today) select p;
if (badData.Any())
{
    throw new Exception("Bad data found");
}

This code compiles but Any() blows up with the following message:

Specified cast is not valid

What am I missing? Is Any() the right method to use in this case?

Edit:
I debugged, and the code blows up before the stored procedure is called, at the following generated line:

        return ((ISingleResult<BadDataResult>)(result.ReturnValue));

To get it to work, I did the following:

I added another stored procedure and dragged it to my context:

CREATE PROCEDURE dbo.BadDataExists
    @AsOfDate DATETIME
AS 
    BEGIN ;
        SET NOCOUNT ON ;
        SELECT  COUNT(*) AS Cnt
        FROM    SomeTable
        WHERE   SOME conditions ;
    END ;

I used this code:

         foreach (var badDataCount in
           context.BadDataExists(asOfDate).Where(badDataCount => badDataCount.Cnt > 0))
             {

Although it works now, I would really like to understand what I was missing.

  • 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-24T17:51:02+00:00Added an answer on May 24, 2026 at 5:51 pm

    I assume your foreach code would also blow up, if context.BadDataForDay(today) would return any data…

    The code that translates the objects from the database into C# objects and returns them as an enumerable inside BadDataForDay seems to be performing a cast that compiles fine but that blows up at runtime, because the object that should be casted has the wrong type.

    The reason why Any and foreach only blow up when data is returned is simple: Without data, no cast will be performed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 and I need to run a SQL Job
I need to run a stored procedure from a C# application. I use the
I'm using LINQ to SQL to get a search result of a FullTextSearch stored
I'm stepping through a stored procedure in SQL Server Management Studio 2008 (SSMS). The
I need to run some precompile steps before I build my project using FlexBuilder.
Using SQL Server 2005 and VB.Net Table1 ID Name Dept 001 Raja IT 002
I'm working on a legacy system, and I need to call a stored procedure
CREATE TABLE DIALOGUE_TABLE(EXPIRE_TIME TIMESTAMP); Following code snippet is inside stored proc : PO_EXPIRETIME :-
I am attempting to use the SQL SERVER 2008 MERGE statement in a stored
I'm attempting to optimize a T-SQL stored procedure I have. It's for pulling records

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.