NHibernate How do I query against an IListstring property?
So because of limitations of the Criteria API, I decided to bend my domain classes to fit.
I created an entity class for the Tag. I couldn't even create it as a value object. It had to have its own id.
I feel dirty now. But being able to construct a dynamic query without resorting to string manipulation was more important to me than staying true to the domain.
QueryOver IListstring property
Well, my suggestion would be:
introduce entity. Even if it would have only one property. Later you can extend that (with Order, IsVisible). And if you will do that everywhere your framework will be built only from
one-to-many
andmany-to-one
relations among first citizens objects. That mean simple "framework generalization, reuse..."
But I see that you do not like it (please, at least try to re-think) - so there is the way:
NHibernate How do I query against an IList property?
Where I tried to show that (based on the documentation) we can use magical word ".elements"
:
17.1.4.1. Alias and property references
So the query which will touch the string elements in your case
Item item = null;
string fact = null;
var demos = session.QueryOver<Item>(() => item)
.JoinAlias(i => i.Facts, () => fact)
// instead of this
// .Add(Restrictions.Eq("fact", "abc"))
// we can use the .elements keyword
.Where(Restrictions.Eq("fact.elements", "abc"))
.List<Item>();
So, this way, you can get Items which do have some facts equal to "abc"
NHibernate QueryOver where value is IN list property/field?
The solution, as described here:
NHibernate: Select item with entry in element bag
should be like this:
var demos = this.session.CreateCriteria<Blog>()
.CreateAlias("DomainAliases", "d")
// .elemnts is what we need
.Add(Restrictions.Eq("d.elements", "hostname string"))
.List<Blog>();
Also check the:
NHibernate How do I query against an IList property?
nhibernate query using criteria for list of value type
Check these Q & A:
- NHibernate: Select item with entry in element bag.
- NHibernate How do I query against an
IList<string>
property?
You will find, that we can access element with a keyword ".elements"
Restrictions.In("category.elements", postedCategories)
with query over:
q.JoinAlias(p => p.CategoryEnum, () => category)
.Where(Restrictions.In("category.elements", postedCategories))
NHibernate ICriteria Restriction find properties in a collection that match a value
I achieved what I wanted by creating an alias:
criteria.CreateAlias("WidgetList", "widgets", JoinType.LeftOuterJoin);
criteria = criteria.Add(Restrictions.Eq("widgets.Bar", myValue));
How to query related objects with nhibernate using contains
After some more fiddling, I got the example (referenced above) to work. I will leave my solution for future reference for others.
public IList<Ad> Search(string query)
{
return unitOfWork.Session
.CreateCriteria<Ad>()
.CreateAlias("Properties", "props")
.Add(Expression.InsensitiveLike("props.Value", query, MatchMode.Anywhere))
.List<Ad>();
}
Hope it helps someone :-)
How to convert nhibernate List to IListT in C#
The issue is that the returned collection l
contains items of type System.Collections.Hashtable
. This is because you used Transformers.AliasToEntityMap
which says:
Each row of results is a map (System.Collections.IDictionary) from alias to values/entities.
To make items of type SalesModel
you need to use Transformers.AliasToBean<SalesModel>()
which:
Creates a result transformer that will inject aliased values into instances of T via property methods or fields.
And then you could cast it or use List<T>()
instead:
IList<SalesModel> list = session
.CreateSQLQuery(@query)
.SetResultTransformer(Transformers.AliasToBean<SalesModel>())
.List<SalesModel>();
Related Topics
Call and Consume Web API in Winform Using C#.Net
Ghost-Borders ('Ringing') When Resizing in Gdi+
Update Requires a Valid Updatecommand When Passed Datarow Collection with Modified Rows
C# Property and Ref Parameter, Why No Sugar
Error Deserializing Xml to Object - Xmlns='' Was Not Expected
No Access to the Session Information Through Signalr Hub. Is My Design Is Wrong
Multiple Identities in ASP.NET Core 2.0
Why Can't I Do Foreach (Var Item in Datatable.Rows)
How to Read to End Process Output Asynchronously in C#
The Remote Server Returned an Unexpected Response: (413) Request Entity Too Large
How to Draw a Rounded Rectangle with Winforms (.Net)
How to Sync the Scrolling of Two Multiline Textboxes