简体   繁体   English

OrderBy列出LINQ查询

[英]OrderBy list LINQ Query

My Model contains List of Offers . 我的模型包含Offers清单。 offers having SpecialOffers value true should be orderd by RGU and OfferPriority . 可提供拥有SpecialOffers价值true应该由orderd RGUOfferPriority The offers having SpecialOffers value false should be ordered by InitialPrice (descending) only. 具有SpecialOffersfalse应仅由InitialPrice (降序)订购。

I tried the following query, it achieves the first part ie ordered by RGU and OfferPriority but this is applied on the non SpecialOffers too. 我尝试了以下查询,它实现了第一部分,即由RGUOfferPriority排序,但这也适用于非SpecialOffers What should be the query to achieve these two tasks? 实现这两项任务的查询应该是什么?

List<OfferModel> providerOffers = Model.Offers
    .Where(x => x.Provider.ProviderCode.Equals(provider))
    .OrderByDescending(o => o.SpecialOffer)
    .ThenByDescending(t => t.RGU)
    .ThenBy(p => p.OfferPriority)
    .Select(x => x)
    .ToList();

EDIT SpecialOffer is a boolean property attached to each offer that determines whether an offer is Special or not 编辑 SpecialOffer是附加到每个SpecialOffer的布尔属性,用于确定商品是否为特价商品

You should first group by this property to get two groups which you can order separately: 您应该首先按此属性进行分组,以获得两个可以单独订购的组:

var offers = Model.Offers.Where(o => o.Provider.ProviderCode.Equals(provider));
var offerGroups = offers.GroupBy(o => o.SpecialOffer);
var specialGroup = offerGroups.Where(g => g.Key == true)
    .SelectMany(g => g)
    .OrderByDescending(o => o.InitialPrice);
var nonSpecialGroup = offerGroups.Where(g => g.Key == false)
    .SelectMany(g => g)
    .OrderByDescending(t => t.RGU)
    .ThenBy(p => p.OfferPriority);
var result = specialGroup.Concat(nonSpecialGroup).ToList(); 

Due to LINQ's deferred execution these queries will be executed as a single sql query. 由于LINQ的延迟执行,这些查询将作为单个sql查询执行。


Disclaimer: i'm not sure if the generated sql will keep the order of the groups, normally you have to apply the ORDER BY on the outer query which is the final CONCAT , it's translated to a UNION ALL . 免责声明:我不确定生成的sql是否会保持组的顺序,通常你必须在外部查询上应用ORDER BY ,这是最终的CONCAT ,它被转换为UNION ALL Then the easiest would be to use Linq-To-Objects with AsEnumerable : 那么最简单的方法是使用带有AsEnumerable Linq-To-Objects

var result = specialGroup.AsEnumerable().Concat(nonSpecialGroup.AsEnumerable()).ToList(); 

You have to split your lists based on their type, you can´t order two different lists with the same statement: 您必须根据类型拆分列表,不能使用相同的语句订购两个不同的列表:

var result1 = model.Offers.Where(x => x.SpecialOffer).OrderBy(x => x.InitialPrice);
var result2 = model.Offers.Except(result1).OrderBy(...);

If not all elements within your list that are not a SpecialOffer should be sorted by ProviderCode , RGU and OfferPriority you can use this for the second list: 如果不是列表中不是SpecialOffer所有元素都应按ProviderCodeRGUOfferPriority排序,则可以将其用于第二个列表:

var result2 = mode.Offers.Where(x => !x.SpecialOffer)
    .Where(x => x.Provider.ProviderCode.Equals(provider))
    .OrderByByDescending(t => t.RGU)
    .ThenBy(p => p.OfferPriority);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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