C# 名称';应用日期';在当前上下文中不存在
我在下面的代码中得到一个错误。似乎找不到解决办法C# 名称';应用日期';在当前上下文中不存在,c#,asp.net,linq,C#,Asp.net,Linq,我在下面的代码中得到一个错误。似乎找不到解决办法 using (var context = new CatLiveDataContext()) { DateTime AppDate; var fieldsaleId = context.FieldSales.Where(fs => fs.CompanyId == companyId && fs.IsClosed).Select(fs
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查询得到的方法时,我得到了错误。您已经在第一条
语句中使用声明了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();
}