Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ Expression to return Property value?
    primarykey
    data
    text
    <p>I'm trying to create a generic function to help me select thousands of records using LINQ to SQL from a local list. SQL Server (2005 at least) limits queries to 2100 parameters and I'd like to select more records than that.</p> <p>Here would be a good example usage:</p> <pre><code>var some_product_numbers = new int[] { 1,2,3 ... 9999 }; Products.SelectByParameterList(some_product_numbers, p =&gt; p.ProductNumber); </code></pre> <p>Here is my (non-working) implementation:</p> <pre><code>public static IEnumerable&lt;T&gt; SelectByParameterList&lt;T, PropertyType&gt;(Table&lt;T&gt; items, IEnumerable&lt;PropertyType&gt; parameterList, Expression&lt;Func&lt;T, PropertyType&gt;&gt; property) where T : class { var groups = parameterList .Select((Parameter, index) =&gt; new { GroupID = index / 2000, //2000 parameters per request Parameter } ) .GroupBy(x =&gt; x.GroupID) .AsEnumerable(); var results = groups .Select(g =&gt; new { Group = g, Parameters = g.Select(x =&gt; x.Parameter) } ) .SelectMany(g =&gt; /* THIS PART FAILS MISERABLY */ items.Where(item =&gt; g.Parameters.Contains(property.Compile()(item))) ); return results; } </code></pre> <p>I have seen plenty of examples of building predicates using expressions. In this case I only want to execute the delegate to return the value of the current ProductNumber. Or rather, I want to translate this into the SQL query (it works fine in non-generic form).</p> <p>I know that compiling the Expression just takes me back to square one (passing in the delegate as Func) but I'm unsure of how to pass a parameter to an "uncompiled" expression.</p> <p>Thanks for your help!</p> <p>**** EDIT:** Let me clarify further:</p> <p>Here is a working example of what I want to generalize:</p> <pre><code>var local_refill_ids = Refills.Select(r =&gt; r.Id).Take(20).ToArray(); var groups = local_refill_ids .Select((Parameter, index) =&gt; new { GroupID = index / 5, //5 parameters per request Parameter } ) .GroupBy(x =&gt; x.GroupID) .AsEnumerable(); var results = groups .Select(g =&gt; new { Group = g, Parameters = g.Select(x =&gt; x.Parameter) } ) .SelectMany(g =&gt; Refills.Where(r =&gt; g.Parameters.Contains(r.Id)) ) .ToArray() ; </code></pre> <p>Results in this SQL code:</p> <pre><code>SELECT [t0].[Id], ... [t0].[Version] FROM [Refill] AS [t0] WHERE [t0].[Id] IN (@p0, @p1, @p2, @p3, @p4) ... That query 4 more times (20 / 5 = 4) </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.
 

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