是否有可能做创建一个通用Session.QueryOver< T&GT ;? [英] Is it possible to do create a generic Session.QueryOver<T>?

查看:212
本文介绍了是否有可能做创建一个通用Session.QueryOver< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于好奇是否有可能做这样的事情使用NHibernate 3

Out of curiosity is it possible to do something like this using NHibernate 3?

public IQueryable<T> FindAll<T>()
{
   return Session.QueryOver<T>().List().AsQueryable();
}



我得到一个编译错误说像...

I get a compilation error saying something like...

类型T必须是为了使用它作为一个参数T引用类型。

The Type T must be a reference type in order to use it as a parameter T.

我在想,如果我可以创造扩展方法来Session.QueryOver来处理泛型类型。

I was wondering if I could create an extension method to Session.QueryOver to handle a generic type.

我可以取代这个使用类似

I could replace this using something like

return Session.CreateCriteria(typeof (T)).List<T>().AsQueryable();



不过,热衷于使用查询API的功能。有任何想法吗?也许缺少明显的东西!

But was keen to use the features of the query api. Any ideas? maybe missing something obvious!!

推荐答案

您缺少对 T的限制

public IQueryable<T> FindAll<T>() where T : class
{
   return Session.QueryOver<T>().List().AsQueryable();
}



,其中T:类定义 T 必须是引用类型。 (由于编译错误要求,apperently QueryOver< T> 被限制为引用类型)。如果一个类型参数已经应用到它的限制,使用该方法具有自身的一个通用的参数的任何一般方法具有施加类似的限制。

where T : class defines that T has to be a reference type. (As the compile error demanded, apperently QueryOver<T> is restricted to reference type). If a type parameter has constraints applied to it, any generic method using this method with a generic parameter of its own has to apply similar constraints.

有关的通用完整概述类型参数的限制,请参见 MSDN

For a complete overview of generic type parameter constraints, see msdn.

这篇关于是否有可能做创建一个通用Session.QueryOver&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆