编程语言
首页 > 编程语言> > c#-System.MissingMethodException:找不到方法:’System.String .get_DayName()’

c#-System.MissingMethodException:找不到方法:’System.String .get_DayName()’

作者:互联网

我有以下类型:

public class TimeBand
{
    public string DayName { get; set; }
    public int customerId { get; set; }
}

我正在创建一个包含TimeBands的列表:

var TimeBandList = new List<TimeBand>
    {
        new TimeBand()
            {
                DayName = DayOfWeek.Monday.ToString(),
                customerId = 10
            },
        new TimeBand()
            {
                DayName = DayOfWeek.Tuesday.ToString(),
                customerId = 11
            }
            .....
    };

我正在使用以下内容将TimeBands加载到另一个列表中:

    var timeBandRange = new List<TimeBand>();

    timeBandRange = TimeBandList.Where
                  (p => p.customerId == newCustomerId  
                     && p.DayName == date.DayOfWeek.ToString()).ToList();

这样做很好,但是在TimeBand类中,我决定将DayName属性的类型从字符串更改为DayOfWeek,因此代码如下所示:

public class TimeBand
{
    public DayOfWeek DayName { get; set; }
    public int customerId { get; set; }
}

var TimeBandList = new List<TimeBand>
    {
        new TimeBand()
            {
                DayName = DayOfWeek.Monday,
                customerId = 10
            },
        new TimeBand()
            {
                DayName = DayOfWeek.Tuesday,
                customerId = 11
            }
            .....
    };

    DateTime date = IndDate;
    var timeBandRange = new List<TimeBand>();

    timeBandRange = TimeBandList.Where
                  (p => p.customerId == parameter.customerId  
                     && p.DayName == date.DayOfWeek).ToList();

现在,此新代码在TimeBandList.Where行上失败,并出现以下错误:System.MissingMethodException:未找到方法:’System.String TimeBand.get_DayName()’.

知道为什么吗?

谢谢

解决方法:

也许您只需要重新编译?我在本地运行此代码,并且运行良好.

class Program
{
    static void Main(string[] args)
    {
        TimeBand.DoSomething();
    }
}


public class TimeBand
{
    public DayOfWeek DayName { get; set; }
    public int customerId { get; set; }

    public static void DoSomething()
    {
        var TimeBandList = new List<TimeBand>
            {
                new TimeBand()
                    {
                        DayName = DayOfWeek.Monday,
                        customerId = 10
                    },
                new TimeBand()
                    {
                        DayName = DayOfWeek.Tuesday,
                        customerId = 11
                    }
            };


            DateTime date = DateTime.Now;
            var timeBandRange = new List<TimeBand>();

            timeBandRange = TimeBandList.Where
                          (p => p.customerId == 1  
                             && p.DayName == date.DayOfWeek).ToList();
                }
            }

标签:dayofweek,c
来源: https://codeday.me/bug/20191030/1971292.html