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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:49:57+00:00 2026-06-11T12:49:57+00:00

I am trying to return LINQ results serialized to JSON from an ASMX Webservice.

  • 0

I am trying to return LINQ results serialized to JSON from an ASMX Webservice. As far as I know this should work so I must be missing something.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCitiesForAffiliate(string aff)
    {
        XDocument centerXml = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Centers.xml"));
        var query = (from center in centerXml.Descendants("Center")
                     where center.Element("ServiceArea").Value == aff
                     orderby center.Element("City") ascending
                     select new { 
                         City = center.Element("City") 
                     }).Distinct();

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }

The serializer.Serialize(query) line throws an ArgumentException: At least one object must implement IComparable. I thought maybe the problem was that I was selecting an anonymous object but typing the object didn’t do it. I’m sure I’m just missing something stupid?

Interestingly, this previous version worked fine:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCitiesForAffiliate(string aff)
    {
        TextFileReader reader1 = new TextFileReader(HttpContext.Current.Server.MapPath("~/App_Data/Centers.csv"), ",");
        var query = (from cols in reader1
                     where cols[3].Equals(aff)
                     orderby cols[2] ascending
                     select new { City = cols[2] }).Distinct();

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }

The TextFileReader object is from this CodeProject project.

  • 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-06-11T12:49:58+00:00Added an answer on June 11, 2026 at 12:49 pm

    Looks like lazy query execution got you. LINQ doesn’t execute your query until the last possible moment. In this case that is on the serialization line. The problem is in your query and not the serializer.

    The problem looks like when you are doing an order by you are getting back an XElement object (which appearantly doesn’t implement IComparable) and blows up trying to compare them.

    I would do your select first then order the results.

    var query = (from center in centerXml.Descendants("Center")
                         where center.Element("ServiceArea").Value == aff
                         select new { 
                             City = center.Element("City") //may need a .ToString() here to get the city name out
                         }).Distinct().OrderBy(x => x.City);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get serialized results from a WCF service from database using
I'm trying to work the kinks out of this LINQ to XML query of
Basically I'm trying to do this in LINQ to SQL; SELECT DISTINCT a,b,c FROM
I am trying to get this LINQ query to return exact matches if they
I'm trying to return a set of distinct values from a DataTable using LINQ.
I'm trying to return a result and a nested result in a linq to
I am trying to use LINQ to return a list of tasks that are
I am trying to write a linq query that will only return certain columns
I'm trying to create linq lambda expression to return customer whose first or last
I am trying to get the below linq query to return -1 if there

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.