title: Multiple dynamic query in Linq
body: <p>First of all, I will explain the table structure</p>
  <pre class="lang-none prettyprint-override"><code>First table Attribute          Second table Category
  
  Id       Name            Id     Name    AttributeId(Foreign Key)
  ------------------      -----------------------------------------
  1      Material          1      Cotton   1
  2      Size              2      Black    3
  3      Color             3      White    3
                           4      Linen    1
  
  Third Table ProductCatLink
  
  Id     CategoryId(Fk Category table)   ProductId(FK of Product Table)
  -----------------------------------------------------------------------------
  1         1                                     5
  2         2                                     6
  3         2                                     5
  4         4                                     6
  5         4                                     7
  6         3                                     8
  </code></pre>
  <p>Consider user selects material cotton and color black and white then result must be</p>
  <pre class="lang-none prettyprint-override"><code> Id       CategoryId     ProductId(ForeignKey of Product Table)
   ---------------------------------------------------------------
      1         1               5
      2         2               5
  </code></pre>
  <p>Consider user selects material cotton and linen and color black and white then result must be</p>
  <pre class="lang-none prettyprint-override"><code>Id       CategoryId     ProductId(ForeignKey of Product Table)
  -------------------------------------------------------------------
      1         1               5
      2         4               6
      3         2               5
      4         1               6
  </code></pre>
  <p>user passes categoryid array from UI like <code>{1,2,3}(cotton material with black or white color)</code> or <code>{1,4,3} (cotton or linen material with black color)</code> must group with attribute id to achieve this.</p>
  <p>I tried different way using contains but not working properly, if checking each product with all category using custom loops works fine but comparing each products with all condition causes performance issue.</p>
  <p>Is there any simple solution for this problem?
  Is there any solution using Predicatebuilder in linq?</p>
  <p>any code snippets on how to achieve this using linq will be helpful</p>
  <p>I done it like this is there any performance issue with it?</p>
  <pre><code> ExpressionStarter&lt;Products&gt; ProductPredicater(params int[] categories)
            {
                  var predicate = PredicateBuilder.New&lt;Products&gt;();
                  var catwithAttributes = from cat in categories.AsEnumerable()
                                          join pSubCat in 
  
                   _productSubCat.GetAll().AsEnumerable()
                                      on cat equals pSubCat.Id                                    
                                      select pSubCat;
  
                var attributeids = catwithAttributes.GroupBy(m =&gt; 
                m.AttributeId).Select(m =&gt; m.Key);
            
  
              foreach (int keyword in attributeids)
              {
                  var subcatlist = catwithAttributes.Where(m =&gt; m.AttributeId == keyword).Select(m =&gt; m.Id).ToList();
                  predicate = predicate.And(p =&gt;  p.ProductCategoryLinkDetails.Any( l=&gt; subcatlist.Contains(Convert.ToInt32(l.ProductSubCategory_Id))));                
              }
  
              return predicate;
          }
  
  
  var result =   from p in products.Get(ProductPredicater(input.ProductCategoryId), 
                 null, m =&gt; m.ProductCategoryLinkDetails) select p;
  </code></pre>