简体   繁体   中英

As I can call a method in the where clause in LINQ 3.5?

As I can call a method in the where clause in LINQ or how I can add a condition to show only the existing files?

Objective: to show list of documents that exist on the server

Query:

var query = from d in dtContext.documents
where FileExists(d.Id, d.Path) == true
select d;

Method:

Private bool FileExists (int ID, string Path)
{
    if(File. Exists(Server.MapPath(Path))
        return true
    else
    {
        using (Model.DataContext dt = new Model.DataContext())
        {
            var vDoc = dt.Documents.Where(x => x.DocumentId == ID).FirstOrDefault();
                vDoc.Status = false;
                dt.SubmitChanges();
        }
        return false;
    }
}

You cannot call a method in a where that cannot be mapped to a provider function (ie can SQL Server execute the function?).

You need to fetch all the documents into memory. If possible, restrict the columns to limit memory usage.

// fetch all documents as an enumerable sequence
var documents = dtContext.Documents.AsEnumerable();
return documents.Where(d => FileExists(d.Id, d.Path));

Alternatively, if you know the pathes where you files are located, you can scan the file system to get list of all the files that exist and pass that to you query.

string[] filePaths = ....
var documents = dtContext.Documents.Where(d => filePaths.Contains(d.Path));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM