C# 多态性与LinqToSql
我有两个应用程序。一个使用C# 多态性与LinqToSql,c#,linq,C#,Linq,我有两个应用程序。一个使用XAF(devexpress框架),第二个使用asp.net mvc。 我的业务类是在XAFapp中创建的。两者都使用相同的数据库。 我在XAF中的课程: public class BaseClass:XPObject { // some fields } // This attribute mean that all fields will be stored in BaseClass table [MapInheri
XAF
(devexpress框架),第二个使用asp.net mvc。
我的业务类是在XAF
app中创建的。两者都使用相同的数据库。
我在XAF中的课程:
public class BaseClass:XPObject
{
// some fields
}
// This attribute mean that all fields will be stored in BaseClass table
[MapInheritance(InheritanceType.ParentTable]
public class Class1:BaseClass
{
// some fields
}
// This attribute mean that all fields will be stored in BaseClass table
[MapInheritance(InheritanceType.ParentTable]
public class Class2:BaseClass
{
// some fields
}
在数据库中,我们有一个表:BaseClass
,其中包含BaseClass
,Class1
中的字段
有这样一个时刻:XAF添加自己的名为ObjectType
的字段(它是XAF自动创建的XPObjectType
表的FK)。
结果我们得到了一个DB:
BaseClass:
ID some_fields_from_BaseClass some_fields_from_Class1 ObjectType
1 some_values some_values 1
XPObjectType:
ID TypeName AssemblyName
1 TestApp.Module.BO.Class1 TestApp.Module
现在,我在ASP.NET MVC应用程序中写道:
Database.BaseClasses.Where( ...some predicate...).ToList();
此查询返回我的基类的集合。但是,我希望该查询返回派生类型(Class1
或Class2
或class3
)
我该怎么做
另外,我不能使用IsDiscriminator
属性,因为它是FK,我不想复制对象(Class1
,class2
,在asp.net mvc应用程序中)。试试这个:
public List<T> GetData<T>(Func<T,bool> fn) where T :BaseClass
{
if (typeof(T) == typeof(BaseClass))
{
List<T> res = null; // Fillout from database
var r = res.Where(fn).Select(o => (BaseClass)o).ToList();
}
else
{
throw new Exception("Invalid type, this service support only BaseClasses");
}
return null;
}
public List GetData(Func-fn),其中T:BaseClass
{
if(typeof(T)=typeof(基类))
{
List res=null;//从数据库中填写
var r=res.Where(fn).Select(o=>(基类)o.ToList();
}
其他的
{
抛出新异常(“无效类型,此服务仅支持基类”);
}
返回null;
}
hmm,此代码将始终返回基类,但我希望我的查询返回派生类型no,您应该像这样调用此函数:var data=GetData(fn)代码>