我创建了3个方法:
public IOrderedEnumerable<JObject> SortByImportance(IOrderedEnumerable<JObject> products)
{
if (products == null) throw new ArgumentNullException(nameof(products));
var firstProduct = products.First();
return !firstProduct.ContainsKey("importance") ?
products :
products.OrderByDescending(m => m["importance"]);
}
public async Task<IOrderedEnumerable<JObject>> SortByPriorityAsync(string categoryId, IOrderedEnumerable<JObject> products, List<Answer> answers)
{
if (string.IsNullOrEmpty(categoryId)) throw new ArgumentNullException(nameof(categoryId));
if (products == null) throw new ArgumentNullException(nameof(products));
if (answers == null) throw new ArgumentNullException(nameof(answers));
var questions = await _questionProvider.Value.ListAsync(categoryId);
if (questions == null) throw new ArgumentNullException(nameof(questions));
if (questions.Count == 0) throw new ArgumentException(nameof(questions));
foreach (var answer in answers)
answer.Question = questions.SingleOrDefault(m => m.Id == answer.QuestionId);
var sortedAnswers = answers.OrderBy(m => m.Question.Priority);
return sortedAnswers.Aggregate(products, (current, answer) => current.ThenByDescending(m => m[answer.Question.Text.ToLower()].ToString().Equals(answer.Text, StringComparison.OrdinalIgnoreCase)));
}
public async Task<IOrderedEnumerable<JObject>> SortBySortationAsync(string categoryId, IOrderedEnumerable<JObject> products)
{
if (string.IsNullOrEmpty(categoryId)) throw new ArgumentNullException(nameof(categoryId));
if (products == null) throw new ArgumentNullException(nameof(products));
var sortations = await _sortationProvider.Value.ListAsync(categoryId);
if (sortations == null) throw new ArgumentNullException(nameof(sortations));
if (sortations.Count == 0) throw new ArgumentException(nameof(sortations));
var orderedSortations = sortations.OrderBy(m => m.Order);
return orderedSortations.Aggregate(products, (current, sortation) => current.ThenByDescending(m => m[sortation.Field].ToString().Equals(sortation.Expression)));
}
这三种方法可以按顺序调用,以改变产品列表的顺序。例如:
var orderedProducts = products.OrderBy(a => 1);
orderedProducts = await sortProvider.SortBySortationAsync(categoryId, orderedProducts);
orderedProducts = await sortProvider.SortByPriorityAsync(categoryId, orderedProducts, answers);
orderedProducts = sortProvider.SortByImportance(orderedProducts);
这段代码运行正常,我的单元测试显示排序正常,但在 SortByImportance
方法中,它报告:
可能会多次枚举
IEnumerable
出现在 products.First()
调用中。
有人能告诉我如何解决这个问题吗?
ThenBy
而不是OrderBy
来维护先前的顺序。 - r3plica