次のように、select句を使用して、データベースクエリから指定された名前フィールドに一致するオブジェクトを選択しようとしています:
objectQuery = from obj in objectList
where obj.Equals(objectName)
select obj;
私のクエリの結果ビューでは、次のようになります。
base {System.SystemException} = {"Boolean Equals(System.Object)"}
Car
、、Make
またはのようなものを期待する必要がある場所Model
誰かが私がここで間違っていることを説明してくれますか?
問題のメソッドは次の場所で確認できます。
// this function searches the database's table for a single object that matches the 'Name' property with 'objectName'
public static T Read<T>(string objectName) where T : IEquatable<T>
{
using (ISession session = NHibernateHelper.OpenSession())
{
IQueryable<T> objectList = session.Query<T>(); // pull (query) all the objects from the table in the database
int count = objectList.Count(); // return the number of objects in the table
// alternative: int count = makeList.Count<T>();
IQueryable<T> objectQuery = null; // create a reference for our queryable list of objects
T foundObject = default(T); // create an object reference for our found object
if (count > 0)
{
// give me all objects that have a name that matches 'objectName' and store them in 'objectQuery'
objectQuery = from obj in objectList
where obj.Equals(objectName)
select obj;
// make sure that 'objectQuery' has only one object in it
try
{
foundObject = (T)objectQuery.Single();
}
catch
{
return default(T);
}
// output some information to the console (output screen)
Console.WriteLine("Read Make: " + foundObject.ToString());
}
// pass the reference of the found object on to whoever asked for it
return foundObject;
}
}
IQuatable<T>
メソッド記述子でインターフェイス " " を使用していることに注意してください。
データベースから取得しようとしているクラスの例は次のとおりです。
public class Make: IEquatable<Make>
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Model> Models { get; set; }
public Make()
{
// this public no-argument constructor is required for NHibernate
}
public Make(string makeName)
{
this.Name = makeName;
}
public override string ToString()
{
return Name;
}
// Implementation of IEquatable<T> interface
public virtual bool Equals(Make make)
{
if (this.Id == make.Id)
{
return true;
}
else
{
return false;
}
}
// Implementation of IEquatable<T> interface
public virtual bool Equals(String name)
{
if (this.Name.Equals(name))
{
return true;
}
else
{
return false;
}
}
}
インターフェイスは次のように簡単に説明されます。
public interface IEquatable<T>
{
bool Equals(T obj);
}