简体   繁体   中英

Linq To SQL DefaultIfEmpty

I have two tables, one that has 450 rows, the other is a Sort Table that will have alot less than that. I want to be able to sort the collection of the tables by using a left join and I want items that are in the 2nd table to be order by an Int column called 'SortOrder'. Like so:

var g_getFragments = (from c in context.Fragments
                                          join fso in context.FragmentSortOrders
                                          on c.ID equals fso.ID into sr
                                          from x in sr.DefaultIfEmpty()
                                         select new { c.ID_Section, c.ID, c.Title, c.IsManagementFragment, SortOrder = x.SortOrder ?? Int32.MaxValue})
                                         .OrderBy(o => o.SortOrder)
                                         .OrderBy(f => f.Title)
                                         .ToList();

This returns all 450 rows as expected, but my order is not right. I want the order to be first by the SortOrder value in the second table, if it is null, then set it to Int32.MaxValue so it will be last. So if I have 1 item in the first table with a value of 1 for SortOrder in the second table, it should be first in the list, but it isn't. It is sorted by title, however.

What am I doing wrong here?

将您的第二个OrderBy更改为ThenBy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM