Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq orderby sum of sublist item compared to mainlist item
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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:</p> <p>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.</p> <pre><code>var result= ( from c in _db.CourseType orderby ((c.TotalSpots * c.CourseInstance.Count) - c.CourseInstance.Sum( ci =&gt; (ci.AmountSignUp ?? 0))) descending select c ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload