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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:39:22+00:00 2026-05-25T10:39:22+00:00

I have a codefirst entity model which has two entities, City and Locality. City

  • 0

I have a codefirst entity model which has two entities, City and Locality. City has one to many relation with Locality. Here is how they are related.

public class City {
    public virtual List<Locality> Localities { get; set; }
}

public class Locality {
    public virtual City City { get; set; }
}

Now I have some code which finds a particular City and loads Localities for that city too. I was looking to sort the loaded Localities by “Name” using LINQ IQueryable something like this

city = context.Cities.AsNoTracking().Include(c => c.Localities.OrderBy(l => l.Name))
                                    .Where(c => c.Id == id).FirstOrDefault();

When I use the above code it throws “ArgumentException”. So I tried the code shown below.

city = context.Cities.AsNoTracking().Include(c => c.Localities)
                                    .Where(c => c.Id == id).FirstOrDefault();
if (city != null) {
     city.Localities.OrderBy(l => l.Name);
}

The above code does not throw any exceptions but it doesn’t sort the the Localities by Name. I get Localities sorted by Id instead.

Any ideas how to sort the “many side” of One-to-Many relationship

  • 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-25T10:39:22+00:00Added an answer on May 25, 2026 at 10:39 am

    LINQ queries do not modify the source collection. You need to create a new sorted City.Localities instance:

    if (city != null) {
        city.Localities = city.Localities.OrderBy(l => l.Name).ToList();
    }
    

    This sorts in memory after the parent and the child collection has been loaded.

    And your code first snippet doesn’t work because you cannot specify sort or filter criteria in an eager loading statement.

    Here is another answer with some more remarks to the same problem: Entity Framework, MVC 3, OrderBy in LINQ To Entities

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

Sidebar

Related Questions

I have an entity model build using EF4.1 code first, which uses a WCF
I have an issue about Entity Framework 4 CTP 5, which I realize LINQ
I am working with Entity Framework Code first. I have a simple model: public
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I've been programming for a while and have used LINQ-To-SQL and LINQ-To-Entities before (although
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
I have a asp.net mvc 2 on .net 4 with code first entity framework
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have a database, and I have entity POCO's, and all I want to

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.