SubSonic 如何在通用方法中提供列名称

发布于 2024-09-24 17:09:05 字数 1313 浏览 1 评论 0原文

使用 SubSonic3,我有这个通用方法(感谢 linq 家伙 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;
    }

它是用类似的方法调用的

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

,其中 IDList 是作为表的键的 guid 列表。

当不是通用时,linq 看起来像这样并且工作完美。令我印象深刻的是,SubSonic 的 linq 提供程序可以采用此代码并将其转换为 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;

我希望能够在 Job 以外的表上调用此方法,并且使用除作业ID。 GetAll Func 之所以有效,是因为它返回与 Job.All( ) 相同的 IQueryable,但 GetID 会引发运行时异常,“不支持 Invoke 类型的 LINQ 表达式节点”。 GetID 返回一个值,但我真正需要的是 Contains( item.JobID) 能够识别为列名并且“where”语法能够接受的值。 (我没有在这里展示,但我对 orderby 也有同样的问题。)

根据您对 SubSonic3 的了解,这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2024-10-01 17:09:05

我的解决方案是传入表达式Where need:

 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 ) ) );

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 ) ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文