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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:24:02+00:00 2026-06-02T08:24:02+00:00

I have a issue I havn’t yet been able to find a solution on.

  • 0

I have a issue I havn’t yet been able to find a solution on. To simplify the issue: I have two SQL tables and I’m using LinQ to SQL. The two tables represents Courses, where CourseType contains CourseName and TotalSpots, and CourseInstance contains CourseDate, AmountSignedUp and a refrence to CourseType. There’s a one to many relation between CourseType and CourseInstance.

Now what I need help with is the Linq query/Lambda expression to sort both CourseType and CourseInstance (if it’s posible). I want the CourseType to be sorted after how many in total free spots (calculated from every CourseInstance under CourseType; CourseType.TotalSpots – CourseInstance.SignedUp), where the CourseType with most free spots shows first. I want CourseInstance to be sorted simlar, by how many free spots that CourseInstance has (CourseType.TotalSpots – CourseInstance.AmountSignedUp).

I can’t get my head around how to solve this. I’ve looked at simlar questions and issue and this is what I’ve made so far, just to give a basic idea:

Edit: I’ve done this code so far which will sort the CourseType correctly. I’m not sure how I can get the CourseInstance sublist sorted (CourseType.CourseInstance). I guess I’ll have to do it later in the code when I’m converting it to a list unless someone has a good solution for this. I could do it as Arion suggested, why I’ll mark his answer.

var result= (
        from c in _db.CourseType
        orderby ((c.TotalSpots * c.CourseInstance.Count) - c.CourseInstance.Sum(
                                ci => (ci.AmountSignUp ?? 0))) descending
        select c
     );
  • 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-02T08:24:03+00:00Added an answer on June 2, 2026 at 8:24 am

    I am not really sure what you want. But kinda sounds like you want to order by the c.TotalSpots - ci.AmountSignedUp. So is not this enough?

    var result=(
        from ct in db.CourseType
        join ci in db.CourseInstance
            on ct.SomeCourseTypeKey equals ci.SomeCourseInstanceKey
        orderby c.TotalSpots - ci.AmountSignedUp
        select new {ci,ct}
        );
    

    where db is the linq data context.

    Or if c.TotalSpots and ci.AmountSignedUp is nullable then like this:

    var result=(
            from ct in db.CourseType
            join ci in db.CourseInstance
                on ct.SomeCourseTypeKey equals ci.SomeCourseInstanceKey
            orderby (c.TotalSpots??0) - (ci.AmountSignedUp??0)
            select new {ci,ct}
            );
    

    I am still not sure want you want but. You could do this:

    var result=(
        from ct in db.CourseType
        orderby ct.TotalSpots
        select new 
        {
            ci,
            CourseInstances=(
                from ci in db.CourseInstance
                where ct.SomeCourseTypeKey == ci.SomeCourseInstanceKey
                orderby ct.TotalSpots- ci.AmountSignedUp
                select ci
            )
        }
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that, while using sql server 2005 i have executed update
I have an issue using jquerys selectors when it comes to a page I
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.
I have issue wuth Firefox not displaying style text-decoration: line-through. I am using jqGrid
Much of my sql code is generated (from POD). Now i have issue where
i have issue regarding Paging using filterexpression. here's the piece of code for filtering
Im writing sql query and i have issue which i cannot fix. I'm trying
MSSQL database. I have issue to create database using old databases data. Old database
i have done drag and drop using with jquery. here i have issue that
I have an issue that is driving me a bit nuts: Using a UserProfileManager

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.