none
How to make this work with PredicateBuilder? RRS feed

  • Question

  • I am concerned with 3 tables:

     

    BuildRequests

    Companies

    RequestCompanies

    I need to return all Build Requests when a Company assigned to that request has a name that Contains "xx"

    BuildRequest has an Id, Company has an Id, since multiple Companies can be assigned to a single request, the assignments are in RequestCompanies with RequestId and CompanyId columns.  I have the query working in Linq (tested in LINQPad):

    from br in BuildRequests
    join rc in RequestCompanies on br.Id equals rc.RequestId
    join c in Companies on rc.CompanyId equals c.Id 
    where c.Name.Contains("ca")
    select br

    but I can't figure out how to work that into an expression which PredicateBuilder can use.  The Lambda expression LINQPad generates is...uhm...well..take a look:

    BuildRequests
       .Join (
          RequestCompanies, 
          br => br.Id, 
          rc => rc.RequestId, 
          (br, rc) => 
             new  
             {
                br = br, 
                rc = rc
             }
       )
       .Join (
          Companies, 
          temp0 => temp0.rc.CompanyId, 
          c => c.Id, 
          (temp0, c) => 
             new  
             {
                <>h__TransparentIdentifier0 = temp0, 
                c = c
             }
       )
       .Where (temp1 => temp1.c.Name.Contains ("ca"))
       .Select (temp1 => temp1.<>h__TransparentIdentifier0.br)
    
    

    I need to use PredicateBuilder because I have multiple criteria being built here, and this is just one field.  All the others are single table references, so they all work just fine.  This is the only one where 2 tables reference a 3rd, and that's what is throwing me.  I tried this:

    predicate = predicate.And(p => p.RequestCompanies.Where(rc => rc.Company.Name.Contains(temp)));

    but I get:

    Error    87    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CMBuildTool.Models.RequestCompany>' to 'bool'    C:\Documents and Settings\dfloyd\My Documents\Visual Studio 2008\Projects\CMBuildTool\CMBuildTool\Models\BuildRequestRepository.cs    77    56    CMBuildTool

    and

    Error    88    Cannot convert lambda expression to delegate type 'System.Func<CMBuildTool.Models.BuildRequest,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type    C:\Documents and Settings\dfloyd\My Documents\Visual Studio 2008\Projects\CMBuildTool\CMBuildTool\Models\BuildRequestRepository.cs    77    56    CMBuildTool

    in VS.  Thanks in advance!

    Wednesday, March 24, 2010 10:38 PM

Answers

  • Hello,

     

    Welcome to LINQ to SQL forum!

     

    Based on your sample codes, the Lambda Expression inside the predicate.And method is Expression<Func<BuildRequest, IEnumerable<RequestCompany>>> instead of Expression<Func<BuildRequest, bool>>.  That’s the reason why we receive two compiler errors.  

     

    PredicateBuilder.And() needs the expression finally returns a Boolean value, so the predicate can be put into .Where() extension method, like the sample codes in the homepage of PredicateBuilder http://www.albahari.com/nutshell/predicatebuilder.aspx.  

     

    If you understand your scenario correctly, you may want such a predicate (Use Any() instead of Where()):

    ===================================================================================

    predicate = predicate.And(p => p.RequestCompanies.Any(rc => rc.Company.Name.Contains(temp)));

    var query = context.BuildRequests.Where(predicate);

    ===================================================================================

     

    Please free feel to let me know if you have any questions.  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by vHaB Thursday, March 25, 2010 1:29 PM
    Thursday, March 25, 2010 4:00 AM
    Moderator