“AppDate”这个名称在当前上下文中不存在

时间:2012-03-12 10:19:46

标签: c# asp.net linq

我在以下代码中收到错误。似乎无法找到解决方案

 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”

当我将appdate传递给linq查询中的方法时,我收到错误。

1 个答案:

答案 0 :(得分:0)

您已在第一个AppDate语句中声明using,因此它不在第一个语句之外的范围内。只需将声明移到该声明之前......或者为了清楚起见,将整个第一个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();
}