LINQ排序依据FK使用左连接依据、LINQ、FK

由网友(Fickle(薄情的))分享简介:我有以下的code:from _categories in context.SCT_Categoriesjoin _categoryOrders in context.SCT_CategoryOrderson _categories.ID equals _categoryOrders.CategoryID int...

我有以下的code:

from _categories in context.SCT_Categories
join _categoryOrders in context.SCT_CategoryOrders
on _categories.ID equals _categoryOrders.CategoryID into joinedData
from _categoryOrders in joinedData.DefaultIfEmpty()
orderby _categoryOrders.OrderIndex descending 
select _categories

这不左连接类别和categoryOrders

Which does a left join on categories and categoryOrders

对于每一个catgoryOrder还有一类。

For every catgoryOrder there is a category.

这工作得很好,只是当我想通过OrderIndex订购(可以为null,999),它把所有空的查询顶部(即空返回的关系,其中一类没有categoryOrder)。

This works well, except that when I want to order by OrderIndex (Can be null to 999) it places all empty (i.e. null returned relationships where a category has no categoryOrder) at the top of the query.

我要如何改变这种把空值在列表的末尾? pfereably $ P $无查询后迭代改变空值999。

How do I change this to put empty values at the end of the list? Prefereably without an iteration after the query to change empty values to 999.

谢谢

JD

推荐答案

我没有尝试这样做(的IQueryProvider可能不喜欢它)

I haven't tried this (the IQueryProvider might not like it)

let orderIndex = _categoryOrders.OrderIndex ?? int.MaxValue

就在您的排序依据,并在orderIndex订单前代替。

Right before your orderby, and order on orderIndex instead.

阅读全文

相关推荐

最新文章