Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First - logically you want Any, not All.</p> <p>Second, this is a poor way to build up a query filter. All of those operations are sent into the database, while the information to determine which filters should be applied is already local. The explicit joins are also bad (association properties could be used instead).</p> <pre><code>IQueryable&lt;WidgetHistory&gt; query = db.widgetHistories .Where(ph =&gt; ph.contact_dt.Value.Date &gt;= startDate &amp;&amp; ph.contact_dt.Value.Date &lt;= endDate); if (!string.IsNullOrEmpty(baanCatFam)) { query = query.Where(ph =&gt; ph.baan_cat_family_code == baanCatFam); } if (!string.IsNullOrEmpty(baanCat)) { query = query.Where(ph =&gt; ph.baan_cat_code == baanCat); } if (!string.IsNullOrEmpty(baanSubCat)) { query = query.Where(ph =&gt; ph.baan_sub_cat_code == baanSubCat); } //TODO sku filtering here. List&lt;MappedItem&gt; widgetItems = from ph in query let c1 = ph.widgetAssignedCode.CCRCode group c1 by c1.code_desc into g select new MappedItem { ItemDescription = g.Key.ToUpper(), ItemCount = g.Count() }).OrderByDescending(m =&gt; m.ItemCount) .ToList(); </code></pre> <p>Third: the answer to your question.</p> <blockquote> <p>what causes this error</p> </blockquote> <p>LinqToSql's query provider cannot translate your local collection into sql. There's only a limitted set of scenarios where it can translate... <code>.Where(ph =&gt; idList.Contains(ph.Id))</code> is translated into an IN clause with 1 parameter per int in idList.</p> <p>To get around this limitation, you need to convert the local collection into an expression. Start by tranforming each item in the collection into a filtering expression:</p> <pre><code>List&lt;Expression&lt;Func&lt;WidgetHistory, bool&gt;&gt;&gt; skuFilters = skuList.Select&lt;string, Expression&lt;Func&lt;WidgetHistory, bool&gt;&gt;&gt;(skuItem =&gt; ph =&gt; ph.ProductMod.StartsWith(skuItem) ).ToList(); </code></pre> <p>Next, a helper method:</p> <pre><code>public static Expression&lt;Func&lt;T, bool&gt;&gt; OrTheseFiltersTogether&lt;T&gt;( this IEnumerable&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; filters) { Expression&lt;Func&lt;T, bool&gt;&gt; firstFilter = filters.FirstOrDefault(); if (firstFilter == null) { Expression&lt;Func&lt;T, bool&gt;&gt; alwaysTrue = x =&gt; true; return alwaysTrue; } var body = firstFilter.Body; var param = firstFilter.Parameters.ToArray(); foreach (var nextFilter in filters.Skip(1)) { var nextBody = Expression.Invoke(nextFilter, param); body = Expression.OrElse(body, nextBody); } Expression&lt;Func&lt;T, bool&gt;&gt; result = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(body, param); return result; } </code></pre> <p>And now putting it all together:</p> <pre><code>if (skuFilters.Any()) //this part goes into where it says "TODO" { Expression&lt;Func&lt;WidgetHistory, bool&gt;&gt; theSkuFilter = skuFilters.OrTheseFiltersTogether() query = query.Where(theSkuFilter); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload