2012-03-12 38 views
0

我在下面的代碼中收到錯誤。似乎無法找到一個解決方案名稱'AppDate'在當前上下文中不存在

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查詢。

回答

0

您已在第一個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(); 
} 
相關問題