icriteria

How do I select a Random Row using NHibernate's ICriteria API?

只愿长相守 提交于 2019-11-27 21:39:25
Can I select a random row using NHibernate's ICriteria API? Just as cundh2o said, it's DBMS-specific. But you can subclass the Order class and define your own custom ordering. For example, for SQL Server: public class RandomOrder: Order { public RandomOrder() : base("", true) {} public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { return new SqlString("newid()"); } } If you are not limited to using ICriteria, I might recommend using HQL instead for selecting a random row, since it may provide more flexibility to use the Random function supplied by your db

NHibernate Criteria - How to filter on combination of properties

佐手、 提交于 2019-11-27 17:00:40
问题 I needed to filter a list of results using the combination of two properties. A plain SQL statement would look like this: SELECT TOP 10 * FROM Person WHERE FirstName + ' ' + LastName LIKE '%' + @Term + '%' The ICriteria in NHibernate that I ended up using was: ICriteria criteria = Session.CreateCriteria(typeof(Person)); criteria.Add(Expression.Sql( "FirstName + ' ' + LastName LIKE ?", "%" + term + "%", NHibernateUtil.String)); criteria.SetMaxResults(10); It works perfectly, but I'm not sure

NHibernate - CreateCriteria vs CreateAlias

岁酱吖の 提交于 2019-11-27 06:45:24
Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing". I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name. Performance wise, is there any difference? Jaguar given these requirements there would be no difference, the generated SQL is the same: for mappings: <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

NHibernate How do I query against an IList<string> property?

橙三吉。 提交于 2019-11-27 02:06:11
I am trying to query against an IList<string> property on one of my domain classes using NHibernate. Here is a simple example to demonstrate: public class Demo { public Demo() { this.Tags = new List<string>(); } public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList<string> Tags { get; set; } } Mapped like this: <class name="Demo"> <id name="Id" /> <property name="Name" /> <bag name="Tags"> <key column="DemoId"/> <element column="Tag" type="String" /> </bag> And I am able to save and retrieve just fine. Now to query for instances of my domain class

NHibernate - CreateCriteria vs CreateAlias

*爱你&永不变心* 提交于 2019-11-26 12:09:09
问题 Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name \"sumthing\". I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name. Performance wise, is there any difference? 回答1: given these requirements there would be no difference, the generated SQL is the same: for