【发布时间】:2012-02-13 10:50:19
【问题描述】:
我有一个多租户应用程序,所有数据都在同一个数据库中。我们通过 objectToTenant 表拆分每个租户的数据。
在通用 Repository 类中,我想实现一个过滤对象的方法,只需指定对象的哪个属性是 tenantToObjectRelationId 以及对象是什么类型(需要,因为不同的类型存储在关系表中)
我想拨打的电话应该是这样的:
// Specialized Repository class
var allItemsForTenant =
this.AllForTenant(item => item.RelationId, 123);
因此我实现了这个方法:
// Generic Repository class
public virtual IQueryable<T> AllForTenant(
Func<T, int> getObjectToTenantRelationId, string objectType)
{
var result =
from item in this.context.GetTable<T>( )
join tenantObject in (
from tenantRelationRecord in objectTenantRelationRepo.All( )
where tenantRelationRecord.TenantId == Global.TenantId
where tenantRelationRecord.ObjectTypeId == type.TypeId
select tenantObjectRecord)
on getObjectToTenantRelationId( item ) equals tenantObject.ObjectId
select item;
return result;
}
但此刻我收到了NotSupportedException
Für die System.Object DynamicInvoke(System.Object[])-Methode gibt es keine unterstützte Übersetzung 在 SQL 中。
是Func<T, int>方法无法翻译成sql造成的。
我需要如何更改Func<T, int> 以便它可以被翻译或有人提供其他解决方案?
【问题讨论】:
-
而不是传入
Func<T, int>传入IQueryable与所有可能的项目并使用它来加入。 -
要使实体框架能够翻译它,您必须传入一个
Expression<Func<T, int>>。
标签: c# .net linq-to-sql generics c#-4.0