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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:48:13+00:00 2026-05-27T07:48:13+00:00

My question is similar to the one here: Select a specific column based on

  • 0

My question is similar to the one here: Select a specific column based on another column's value but in LINQ, using VS2010, C#, .Net 4.0 and LINQ to Entities

Here’s the snippet I’m having an issue with:

var BillingNumbers = (from o in mycontext.MyOrders
                    where o.Status.Description.Trim().ToUpper().Equals("SHIPPED")
                    && (o.ActualShipDate >= date1.Date && o.ActualShipDate < date2.Date)
                    select new
                            {BillingNumber = (
                          o.SiteID == "NYC"? o.NYCBillingNumber.Trim():
                          o.SiteID == "DAL" ? o.DallasBillingNumber.Trim(): "unknown") 
                          });
             foreach (var billingnumber in BillingNumbers)
             {
                sqlcmd = MyDatabase.GetStoredProcCommand("MyBillingNumberSP") as SqlCommand;
                sqlcmd.CommandTimeout = Int32.Parse(Settings.Default.SQL_COMMAND_TIMEOUT.ToString());
                MyDatabase.AddInParameter(sqlcmd, "@BillingNumber", DbType.String, billingnumber);
                MyDatabase.AddOutParameter(sqlcmd, "@MyOutputParam", DbType.Boolean, 1);
                LobjDS = MyDatabase.ExecuteDataSet(sqlcmd);  //GETTING AN ERROR HERE
                bool JobbingApplied = Convert.ToBoolean(MyDatabase.GetParameterValue(sqlcmd, "@MyOutputParam"));
                Console.WriteLine(billingnumber);
             }

The ExecuteDataSet command throws an InvalidCastException with the message:

Failed to convert parameter value from a <>f__AnonymousType0`1 to a String.

What is wrong and why is this anonymous type throwing an error when attempted to interpret it as string?

  • 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-27T07:48:13+00:00Added an answer on May 27, 2026 at 7:48 am

    Replace

    MyDatabase.AddInParameter(sqlcmd, "@BillingNumber", DbType.String, billingnumber);
    

    with

    MyDatabase.AddInParameter(sqlcmd, "@BillingNumber", DbType.String, billingnumber.BillingNumber);
    

    var billingnumber is a anonymous type with one string property BillingNumber. You need to reference that property, it is not automatically convertible to its first member.

    An alternative is to replace

    select  new
    {   BillingNumber = (
        o.SiteID == "NYC"? o.NYCBillingNumber.Trim():
        o.SiteID == "DAL" ? o.DallasBillingNumber.Trim(): "unknown") 
    }
    

    with

    select (
        o.SiteID == "NYC"? o.NYCBillingNumber.Trim():
        o.SiteID == "DAL" ? o.DallasBillingNumber.Trim(): "unknown")
    

    Then you don’t need the anonymous type at all.

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

Sidebar

Related Questions

My question is similar this one here: ResultsController to another ResultsController A typical app
I have a question similar to the one here: Event handlers inside a Javascript
This is a similar question to this one here . Given a list of
Liu Chang asked a very similar question to this one here, Linux equivalent of
I found a few questions similar to this one here on SO, but none
My question is similar to this one , but I would like to replicate
Similar question as this one but for a Microsoft Environment. Email --> Exchange Server
I saw this similar question here but can't figure out how to use Contains
I've looked for a similar question, but the only one that was close didn't
I asked a question similar to this one here and was given great answers,

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.