C# 序列在 Linq 语句中不包含匹配元素错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14433147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:47:22  来源:igfitidea点击:

Sequence contains no matching element error in Linq Statement

c#.netlinq

提问by jomsk1e

I am trying to debug my method but I don't know what's wrong with it.

我正在尝试调试我的方法,但我不知道它有什么问题。

There are times that it throws an error and sometime it's okay. I don't know what's wrong.

有时它会抛出错误,有时也没关系。我不知道怎么了。

Here's my method:

这是我的方法:

private void GetWorkingWeek(int month, int year)
    {
        var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;


        var daysInMonth = Enumerable.Range(1, cal.GetDaysInMonth(year, month));

        var listOfWorkWeeks = daysInMonth
            .Select(day => new DateTime(year, month, day))
            .GroupBy(d => cal.GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
            .Select(g => Tuple.Create(g.Key, g.First(), g.Last(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)))
            .ToList();
         foreach (var weekGroup in listOfWorkWeeks)
                {
                    Console.WriteLine("Week{0} = {1} {2} to {1} {3}"
                        , weekNum++
                        , System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month)
                        , weekGroup.Item2.Day
                        , weekGroup.Item3.Day);
                }
    }

This is the line where the error appear:

这是出现错误的行:

var listOfWorkWeeks = daysInMonth
            .Select(day => new DateTime(year, month, day))
            .GroupBy(d => cal.GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
            .Select(g => Tuple.Create(g.Key, g.First(), g.Last(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)))
            .ToList();

This is the error:

这是错误:

 InvalidOperationException : Sequence contains no matching element

采纳答案by TKharaishvili

var listOfWorkWeeks = daysInMonth
        .Select(day => new DateTime(year, month, day))
        .GroupBy(d => cal.GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
        .Select(g => Tuple.Create(g.Key, g.FirstOrDefault(), g.LastOrDefault(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)))
        .ToList();

Try using FirstOrDefaultand LastOrDefaultinstead of Firstand Last, these methods will return the default value of the type they are invoked for if no elements match the lambda expression you provide as a parameter.

尝试使用FirstOrDefaultandLastOrDefault代替Firstand Last,如果没有元素与您作为参数提供的 lambda 表达式匹配,这些方法将返回调用它们的类型的默认值。

In case of g.FirstOrDefault(), the default value will be returned if g is empty and in case of g.LastOrDefault(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)the default value will be returned if all days are either saturday or sunday.

在 的情况下g.FirstOrDefault(),如果 g 为空,g.LastOrDefault(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)则返回默认值,如果所有天都是星期六或星期日,则返回默认值。