LINQ搜索查询

时间:2011-06-03 11:02:21

标签: linq-to-sql

下面的函数返回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.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>();
        }

我在这里做的任何错误?

提前致谢!

1 个答案:

答案 0 :(得分:2)

你需要将Where返回给IQueryable ....(即:files = 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>();
    }