名称';应用日期';在当前上下文中不存在

本文关键字:上下文 不存在 日期 应用 名称 | 更新日期: 2023-09-27 18:27:24

我在下面的代码中得到一个错误。似乎找不到解决方案

 using (var context = new CatLiveDataContext()) 
            {
                DateTime AppDate;
                var fieldsaleId = context.FieldSales.Where(fs => fs.CompanyId == companyId && fs.IsClosed).Select(fs =>(int?) fs.Id).SingleOrDefault();
                if (fieldsaleId != null)
                {
                var fieldsale = context.FieldSales.Where(fs => fs.Id == fieldsaleId).SingleOrDefault();
                var calenderitem = fieldsale.CalendarItem;
                    if (calenderitem != null)
                    {
                        AppDate = calenderitem.StartTime;
                    }
                    else
                    {
                        AppDate = DateTime.Today;
                    }
                }
            }
            using (var repository = new TaskRepository())
            {
                repository.CreateDesiredDirectoryTask(companyId, directoryName, directoryEdition, directoryHeading, userStaffId, AppDate);
                repository.SubmitChanges();
            }

错误:名称"AppDate"在当前上下文中不存在

当我从linq查询中将appdate传递给方法时,我得到了错误。

名称';应用日期';在当前上下文中不存在

您已经在第一个using语句中声明了AppDate,所以它不在第一个语句之外的范围内。把声明移到声明之前。。。或者为了清楚起见,将第一个using语句的全部放入一个单独的方法中:

// Rename according to real meaning
DateTime appDate = FetchAppDate(companyId, fieldsaleId);
using (var repository = new TaskRepository())
{
    repository.CreateDesiredDirectoryTask(companyId, directoryName,
        directoryEdition, directoryHeading, userStaffId, AppDate);
    repository.SubmitChanges();
}