2011-06-03 112 views
0

下面的函數返回文件表中的所有行,即使如果應用,其中過濾器,LINQ搜索查詢

public IList<File> SearchFiles(int? FileID, int? type, int? Status) 
     { 

      var files = from fil in _context.Files 
          select fil; 

      if (FileID != null) 
      { 
       files.Where(x => x.FileID == FileID); 
      } 

      if (type != null) 
      { 
       files.Where(x => x.FileTypeID == type); 
      } 

      if (Status != null) 
      { 
       files.Where(x => x.FileStatusID == Status); 
      } 

      return files.ToList<File>(); 
     } 

任何錯誤我在做什麼嗎?

在此先感謝!

回答

2

您需要分配在哪裏回的IQueryable ....(即:文件= files.where(....

public IList<File> SearchFiles(int? FileID, int? type, int? Status) 
    { 

     var files = from fil in _context.Files 
         select fil; 

     if (FileID != null) 
     { 
      files = files.Where(x => x.FileID == FileID); 
     } 

     if (type != null) 
     { 
      files = files.Where(x => x.FileTypeID == type); 
     } 

     if (Status != null) 
     { 
      files = files.Where(x => x.FileStatusID == Status); 
     } 

     return files.ToList<File>(); 
    }