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:
var Results =
from j in Job.All( )
where guids.Contains( j.JobID )
orderby j.JobNumber
select j;
I am using SubSonic, so Job.All( ) is a static method that returns an IQueryable<Job>
. guids is an IEnumerable<Guid>
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).
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.
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.
Note: I have no objection to using the lambda-style notation. This is my first cut, which has problems with "T.All( )", and item.???:
const int MAX_ITEMS = 2000;
public List<T> GetFromList<T>( List<Guid> _IDs )
where T : class, IActiveRecord
{
List<T> rc;
if ( MAX_ITEMS > _IDs.Count )
{
var Results =
from item in T.All( )
where _IDs.Contains( item.??? )
orderby item.???
select item;
rc = Results.ToList<T>( );
}
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<T>( );
}
return rc;
}