私はまだNHibernateとLinqを学んでいるので、この質問をするときの私の無知を許してください。私はいくつかの検索を行いましたが、私の質問に対する解決策が可能かどうか、またはその方法がわかりません
次のコードブロックがあります。
// this function searches the database's table for a single object that matches the 'Name' property with 'objectName'
public static Object Read<T>(string objectName)
{
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
object foundObject = null; // 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.Name == objectName select obj);
// make sure that 'objectQuery' has only one object in it
try
{
foundObject = objectQuery.Single();
}
catch
{
return null;
}
// 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;
}
}
これは、1行を除いてすべて素晴らしいです:
objectQuery = (from obj in objectList where obj.Name == objectName select obj);
ここでの問題は、不明なオブジェクトの「名前」プロパティを要求していることです。これは不可能であるだけでなく、コードの誤りでもあります。
ここで本当にやりたいことは、T タイプ オブジェクトに属するプロパティを持つ項目を探していることを指定することです。
テイカーはいますか?