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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:12:32+00:00 2026-05-22T22:12:32+00:00

i have written the following query and it is giving error Unable to cast

  • 0

i have written the following query and it is giving error Unable to cast object of type

System.Data.Objects.ObjectQuery'[ITClassifieds.Models.Viewsearch]' to type 'ITClassifieds.Models.Viewsearch'.

my code is as follows

 if (zipcode.Contains(","))//opening of zipcode conatins comma
 {
    do
    {
        zipcode = zipcode.Replace("  ", " ");
        zipcode = zipcode.Replace(", ", ",");

    } while (zipcodecity.Contains("  "));
    char[] separator = new char[] { ',' };
    string[] temparray = zipcode.Split(separator);
    var zipcd = (from u in db.ZipCodes1
                 where u.CityName == temparray[0] && u.StateAbbr == temparray[1] && u.CityType == "D"
                 select new Viewsearch
                 {
                     Zipcode = u.ZIPCode


                 }).Distinct();
    Viewsearch vs = (Viewsearch)zipcd;
    if (zipcd.Count() > 0)
    {
        zipcode = vs.Zipcode;
        locations = "";
    }
    else
    {
        tempStr = "";
        zipcode = "";
    }
}
  • 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-22T22:12:32+00:00Added an answer on May 22, 2026 at 10:12 pm

    The Distinct method returns an enumerable collection (in your case, and ObjectQuery<T>, which may contain more than one element. You can’t typecast that directly to an item in the collection, you need to use one of the IEnumerable methods to get it:

    Viewsearch vs = zipcd.SingleOrDefault();
    if ( vs != null )
    {
        zipcode = vs.Zipcode;
        locations = String.Empty;
    }
    else
    {
        zipcode = String.Empty;
        tempStr = String.Empty;
    }
    

    SingleOrDefault will throw an exception if there is more than one item in the collection; if that’s a problem, you can also use FirstOrDefault to grab the first item, as one example.

    Also, unrelated to your question, but you don’t need the temporary array variable for your string separators. The parameter to the Split method is a params array so you can just call it like this:

    string[] temparray = zipcode.Split(',');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code but it is giving parse error . here
I have written the following query, but in the WHERE clause I have used
I have written a DB2 query to do the following: Create a temp table
I have following query written in HQL for Hibernate. ======================================================================== select new map(ret.retailerDesc as
I have just written the following query and gotten a surprise (to me) result;
I have written the following query: Private Sub Size_Sqft_BeforeUpdate(Cancel As Integer) Me!Size_Sqft = Nz(Me!Size_Sqft,
We have written the following query which returns records in Oracle but when the
I have written the select fallowing query but it is giving different results surprisingly
I have written the following query using the Aggregate function SUM. SELECT D.PODetailID, SUM(D.AcceptedQty)
I have written the following query: SELECT CLIENT.CLIENTNO, CLIENT.CNAME, TOTALS.TOTAL FROM CLIENT, (SELECT CLIENTNO,

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.