views:

37

answers:

1

Using SubSonic3, I have this generic method (thanks to linq guy, James Curran):

    public List<T> GetFromList<T>( List<Guid> _IDs,
        Func<T, Guid> GetID,
        Func<IQueryable<T>> GetAll )
            where T : class, IActiveRecord
    {
        List<T> rc = null;
        var Results =
            from item in GetAll( )
            where ( _IDs as IEnumerable<Guid> ).Contains( GetID( item ) )
            select item;

        rc = Results.ToList<T>( );
        return rc;
    }

It is called with something like

List<Job> jresults = GetFromList( IDList,
    item => item.JobID,
    ( ) => Job.All( ) );

Where IDList is a List of guids that are keys to the table.

When not generic, the linq looks like this and works perfectly. I was quite impressed that SubSonic's linq provider could take this code and turn it into SELECT * FROM Job WHERE JobID IN (a, b, c):

            var Results =
            from item in Job.All( )
            where ( _IDs as IEnumerable<Guid> ).Contains( item.JobID )
            select item;

I want to be able to call this method on tables other than Job, with keys other than JobID. The GetAll Func works because it returns the same IQueryable that Job.All( ) does, but GetID throws a run-time exception, "LINQ expression node of type Invoke is not supported". GetID returns a value, but what I really need from it is something that Contains( item.JobID) would recognize as a column name and that the "where" syntax would accept. (I don't show it here, but I have the same problem with orderby.)

Is that possible, with what you know of SubSonic3?

A: 

My solution was to pass in the expression that Where needed:

 public List<T> GetFromList( List<Guid> _IDs,
    Func<IQueryable<T>> GetAll,
    Expression<Func<T, bool>> _where )
        where T : class, U, IActiveRecord
{
    List<T> rc = new List<T>( );
    if ( 0 < _IDs.Count )
    {
        if ( MAX_ITEMS > _IDs.Count )
        {
            var Results = GetAll( ).Where( _where );
            rc = Results.ToList( );
        }
        else
        {
            var Results =
                from id in _IDs
                join item in GetAll( ) on id equals item.KeyValue( )
                select item;
            rc = Results.ToList( );
        }
    }
    return rc;
}

called by

  rc = GetFromList(
      IDList,
      ( ) => Job.All( ),
      ( item => ( IDList as IEnumerable<Guid> ).Contains( item.JobID ) ) );
Kelly