简体   繁体   中英

Linq query syntax with PredicateBuilder?

Is it possible to use PredicateBuilder with LINQ query syntax? For example, building a basic search method, with many optional parameters. How can I add a where clause based on the predicate I have built?

public List<Entities.ProjectSearchResult> GetProjects(string projectName, 
                                                  string projectLaunchName, 
                                                  int? projectID, int? categoryID,
                                                  int? subCategoryID)
{
    var predicate = PredicateBuilder.True<Entities.Project>();
    if (!String.IsNullOrWhiteSpace(projectName))
        predicate = predicate.And(p => p.ProjectName.Contains(projectName));
    if (!String.IsNullOrWhiteSpace(projectLaunchName))
        predicate = predicate.And(p => p.ProjectName.Contains(projectLaunchName));
    if (projectID.HasValue)
        predicate = predicate.And(p => p.ProjectId == projectID.Value);
    if (categoryID.HasValue)
        predicate = predicate.And(p => p.SectorCode == categoryID.Value);
    if (subCategoryID.HasValue)
        predicate = predicate.And(p => p.SubSectorCode == subCategoryID.Value);

    using (CHIPSDbContext db = new CHIPSDbContext())
    {
        var query = (from p in db.Projects
                     join s in db.ProjectStatus on p.ProjectStatusCode equals s.ProjectStatusCode
                     join b in db.ProjectBrands on p.ProjectId equals b.ProjectId into brandList
                     from sublist in brandList.DefaultIfEmpty()
            select new Entities.ProjectSearchResult
            {
                ProjectID = p.ProjectId,
                ProjectName = p.ProjectName,
                ProjectLaunchName = p.ProjectLaunchName,
                Status = s.ProjectStatusDesc,

            }).AsExpandable().ToList();
        return query;
     }
}

Build the predicate on the type you return instead, so that it's more clear from the calling method what you filter on.

var predicate = PredicateBuilder.True<Entities.ProjectSearchResult>(); // Expression with your type
if (!String.IsNullOrWhiteSpace(projectName))
    predicate = predicate.And(p => p.ProjectName.Contains(projectName));    
...
using (CHIPSDbContext db = new CHIPSDbContext())
{
    var query = (from p in db.Projects
                 join s in db.ProjectStatus on p.ProjectStatusCode equals s.ProjectStatusCode
                 join b in db.ProjectBrands on p.ProjectId equals b.ProjectId into brandList
                 from sublist in brandList.DefaultIfEmpty()
        select new Entities.ProjectSearchResult
        {
            ProjectID = p.ProjectId,
            ProjectName = p.ProjectName,
            ProjectLaunchName = p.ProjectLaunchName,
            Status = s.ProjectStatusDesc,

        })
         .Where(predicate.Expand()) // Don't forget to expand predicate
         .AsExpandable()
         .ToList();
    return query;
 }

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