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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:14:31+00:00 2026-06-15T18:14:31+00:00

So I am trying to order a query by an int var that is

  • 0

So I am trying to order a query by an int var that is in an ordered list of the same int vars; e.g. the query must be sorted by the lists order of items. Each datacontext is from a different database which is the reason i’m making the first query into an ordered list of id’s based on pet name order, only the pet id is available from the second query’s data fields, Query looks like:

        using (ListDataContext syndb = new ListDataContext())
        {
            using (QueryDataContext ledb = new QueryDataContext())
            {   
                // Set the order of pets by name and make a list of the pet id's
                var stp = syndb.StoredPets.OrderBy(x => x.Name).Select(x => x.PetID).ToList();
                // Reorder the SoldPets query using the ordered list of pet id's 
                var slp = ledb.SoldPets.OrderBy(x => stp.IndexOf(x.petId)).Select(x => x);

                // do something with the query

            }
        }

The second query is giving me a “Method ‘Int32 IndexOf(Int32)’ has no supported translation to SQL.” error, is there a way to do what I need?

  • 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-15T18:14:32+00:00Added an answer on June 15, 2026 at 6:14 pm

    LINQ to SQL (EF) has to translate your LINQ queries into SQL that can be executed against a SQL server. What the error is trying to say, is that the .NET method of IndexOf doesn’t have a SQL equivalent. You may be best to get your data from your SoldPets table without doing the IndexOf part and then doing any remaining ordering away from LINQ to SQL (EF).

    Something like this should work:

    List<StoredPet> storedPets;
    List<SoldPet> soldPets;
    
    using (ListDataContext listDataContext = new ListDataContext())
    {
        using (QueryDataContext queryDataContext= new QueryDataContext())
        {   
            storedPets =
                listDataContext.StoredPets
                    .OrderBy(sp => sp.Name)
                    .Select(sp => sp.PetId)
                    .ToList();
    
            soldPets =
                queryDataContext.SoldPets
                    .ToList();
        }
    }
    
    List<SoldPets> orderedSoldPets =
        soldPets.OrderBy(sp => storedPets.IndexOf(sp.PetId))
    

    Note: Your capitalisation of PetId changes in your example, so you may wish to look at that.

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

Sidebar

Related Questions

Trying HQL, total NHibernate n00b. public IEnumerable<Log> GetLast(int numRecords, string severity) { var query
I'm trying order a Linq to NHibernate query by the sum of it's children.
I'm trying the following query results = Tickets.order(created_at DESC).where({ (title =~ params[:title]) & (name
I currently am trying the following code: $sql = $db->query(select id,username,fullname from userss ORDER
I'm trying to order models in a Django app using a field that has
I am trying to write a higher-order Racket function that takes a first-order function
I am trying to optimize a sql query which is using order by clause.
I am trying to sort a linq query so that the results are returned
I'm trying to query a table which has a column that contains straight xml
I am trying to get 4 List deep List collection, List<List<List<List<int>>>> . From my

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.