C#Select子句返回系统异常而不是相关对象

Kas*_*hif 0 c# sql database select iqueryable

我试图使用select子句从数据库查询中挑选一个与指定名称字段匹配的对象,如下所示:

objectQuery = from obj in objectList
              where obj.Equals(objectName)
              select obj;
Run Code Online (Sandbox Code Playgroud)

在我的查询的结果视图中,我得到:

base {System.SystemException} = {"Boolean Equals(System.Object)"}

在那里我应该期待有点像Car,MakeModel

有人请解释我在这里做错了什么吗?


有问题的方法可以在这里看到:

// 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接口简单描述为:

public interface IEquatable<T>
{
    bool Equals(T obj);
}
Run Code Online (Sandbox Code Playgroud)

das*_*ght 6

IQueryable<T>对后备数据存储执行查询(在本例中,它是SQL RDBMS).您的SQL RDBMS不知道IEquatable<T>*,并且不能使用它的实现:查询函数必须可以转换为SQL,并且obj.Equals(objectName)不可翻译.

您可以转换IQueryable<T>IEnumerable<T>内存中的查询并执行查询,但这样效率太低.您应该更改签名以获取Expression<Func<TSource, bool>>谓词,并将名称检查器传递给它:

public static T Read<T>(Expression<Func<T,bool>> pred) {
    using (ISession session = NHibernateHelper.OpenSession()) {
        return session.Query<T>().SingleOrdefault(pred);
    }
}
Run Code Online (Sandbox Code Playgroud)

您现在可以使用此方法,如下所示:

Make snake = Read<Make>(x => x.Name == "snake");
Run Code Online (Sandbox Code Playgroud)


*此外,您IEquatable<T>没有在您显示的Equals(string)方法中使用:该方法将被视为实现IEquatable<string>,但在您的Make类实现的接口列表中未提及.