QueryOver IList< string>财产 [英] QueryOver IList<string> property

查看:126
本文介绍了QueryOver IList< string>财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有ICollection属性的映射类,该属性被映射为Set(通过代码映射使用).请注意,该集合包含字符串,而不包含另一个映射的实体.例如

I have a mapped class that has a ICollection property which is mapped as a Set (using by code mappings). Note that the collection contains strings and not another mapped entity. e.g.

public class Item
{
    public virtual ICollection<string> Facts { get; set; }  
}

public class ItemMapping
{
    public ItemMapping()
    {
        Set(x => x.Facts, m =>
        {
            m.Key(k => k.Column("ItemId"));
            m.Table("Facts");
        }, col => col.Element(m =>
        {
            m.Column("Description");
            m.Type(NHibernateUtil.String);
        }));
    }
}

这有效,对具有事实的项目进行CRUD操作也可以.

This works and CRUD operations on Items with Facts works fine.

但是,我想查询数据库中的事实(例如,检索计数或前20个事实或检索一些随机事实),但是鉴于没有实体,我该怎么做?我不想引入Fact实体,因为它仅有的属性是字符串.

However, I want to QueryOver<> the Facts in the database (e.g. retrieve the count or first 20 facts or retrieve some random facts) but given there is no entity how do I do this? I don't want to introduce a Fact entity because the only property it would have would be a string.

推荐答案

好吧,我的建议是:

介绍实体.即使它只有一个属性.以后您可以扩展它(使用Order,IsVisible).而且,如果您将在所有地方执行此操作,则仅根据第一个公民对象之间的one-to-manymany-to-one关系来构建框架.这意味着简单的框架概括,重用..."

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 and many-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:

在我试图证明(基于文档)的地方,我们可以使用神奇的词 ".elements" :

Where I tried to show that (based on the documentation) we can use magical word ".elements":

因此查询将根据您的情况触及字符串元素

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>();

因此,通过这种方式,您可以获得具有与"abc"

So, this way, you can get Items which do have some facts equal to "abc"

这篇关于QueryOver IList&lt; string&gt;财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆