Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试创建排序方法时,可能会多次枚举IEnumerable_C# - Fatal编程技术网

C# 尝试创建排序方法时,可能会多次枚举IEnumerable

C# 尝试创建排序方法时,可能会多次枚举IEnumerable,c#,C#,我创建了3种方法: public IOrderedEnumerable<JObject> SortByImportance(IOrderedEnumerable<JObject> products) { if (products == null) throw new ArgumentNullException(nameof(products)); var firstProduct = products.First(); return !first

我创建了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)));
}
这很好,我的单元测试显示排序很好,但在
SortByImportance
方法中,它说明:

IEnumerable的可能多重枚举

products.First()
调用中。
有人能告诉我如何解决这个问题吗?

如果这是一种可取的行为,您可以随时用适当的注释抑制警告

或者,你可以做一个小把戏:

var orderedProducts = products.OrderBy(a => 1) 
                       .ToList() // do the sorting
                       .OrderBy(a => 1); //convert to IOrderedEnumerable

警告仍然存在,但第二次排序将是排序的最佳方案,只需将IEnumerable转换为IORDerenumerable(可能有一次枚举)。

Try
var firstProduct=products.ToList().First()

您可以将您的收藏转换为列表(),并防止出现此警告。如果我这样做,我将无法使用我的方法,因为它们使用
ThenBy
而不是
OrderBy
来维护以前的订单“return firstProduct.ContainsKey(“重要性”)?products.OrderByDescending(m=>m[“重要性”):products;”更容易阅读这实际上不是一个“可能的”多重枚举;您正在多次枚举它。。。
var orderedProducts = products.OrderBy(a => 1) 
                       .ToList() // do the sorting
                       .OrderBy(a => 1); //convert to IOrderedEnumerable