开发者

LINQ search query

开发者 https://www.devze.com 2023-03-10 00:25 出处:网络
The below function returns all the rows in Files table even when the where filter if applied, public IList<File> SearchFiles(int? FileID, int? type, int? Status)

The below function returns all the rows in Files table even when the where filter if applied,

 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>(开发者_运维知识库);
        }

Any mistake i am doing here?

Thanks in advance!


You need to assign the Where back to the IQueryable.... (ie: 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>();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消