nhibernate queryover.where error
I recently started playing withe nhibernate and am currently trying to implement a generic function that search the database for entries with a specific name:
public T FindName<T>(string name) where T : class
{
T obj;
using (ISession session = m_SessionFactory.OpenSession())
{
obj = session.QueryOver<T>()
.Where(x => x.Name == name).SingleOrDefault();
}
return 开发者_StackOverflow中文版obj;
}
The problem is that I get a complaint for the ".Where(x => x.Name == name)" statement: "Delegate 'System.Func' does not take 1 arguments". From what I saw on the web, this statement is used quite often and I am not sure what I am doing wrong.
Please let me know if you have any ideas.
Many Thanks.
You'd need to ensure that you can call Name on x of type T.
This can be achieved by making an interface with a property Name, type string, and specifying...
where T : class, INameable
Here's some code:
interface INameable
{
string Name { get; }
}
class MyClass
{
ISessionFactory m_SessionFactory;
public T FindName<T>(string name) where T : class, INameable
{
T obj;
using (ISession session = m_SessionFactory.OpenSession())
{
obj = session.QueryOver<T>()
.Where(x => x.Name == name).SingleOrDefault();
}
return obj;
}
精彩评论