Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneralizing the column name in linq.Where or .OrderBy
    primarykey
    data
    text
    <p>I have a "manager" class with a number of sub-classes. I find one particular method being duplicated in all or almost all of the sub-classes, so I want to generalize it. In one case, it looks like this:</p> <pre><code> var Results = from j in Job.All( ) where guids.Contains( j.JobID ) orderby j.JobNumber select j; </code></pre> <p>I am using SubSonic, so Job.All( ) is a static method that returns an <code>IQueryable&lt;Job&gt;</code>. guids is an <code>IEnumerable&lt;Guid&gt;</code> which holds a list of keys into the Job table. My final result is a SQL query with WHERE Job.JobID IN (x, y, z).</p> <p>What I'm looking for is a way to call a method like this where I pass in guids, "Job", "JobID" and "JobNumber" so that I can plug them into the appropriate places.</p> <p>There is a limit to the number of parameters a SQL IN clause can handle, so my general method would also check guids.Count and code a little bit different query when it exceeded a certain number (say, 2000). This code is also in all the sub-classes.</p> <p>Note: I have no objection to using the lambda-style notation. This is my first cut, which has problems with "T.All( )", and item.???:</p> <pre><code> const int MAX_ITEMS = 2000; public List&lt;T&gt; GetFromList&lt;T&gt;( List&lt;Guid&gt; _IDs ) where T : class, IActiveRecord { List&lt;T&gt; rc; if ( MAX_ITEMS &gt; _IDs.Count ) { var Results = from item in T.All( ) where _IDs.Contains( item.??? ) orderby item.??? select item; rc = Results.ToList&lt;T&gt;( ); } else // too many for IN clause { var Results = from id in _IDs join item in T.All( ) on id equals item.??? orderby item.??? select item; rc = Results.ToList&lt;T&gt;( ); } return rc; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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