SubSonic 如何在通用方法中提供列名称
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的解决方案是传入表达式Where need:
Called by
My solution was to pass in the expression that Where needed:
called by