如何通过一个ICollection的在MVC3 NHibernate的迭代 [英] how to iterate through an Icollection In MVC3 nhibernate

查看:93
本文介绍了如何通过一个ICollection的在MVC3 NHibernate的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发中MVC3的应用程序。
结果我有两个下拉列表和值在第一个下拉选择的基础上填充第二个下拉。
第一下拉是场和场的基础上,选择了第二下拉填充状态,其中所述过程是可用的。
搜索结果Foreg.if当然是'MCA'状态应该是马哈拉施特拉邦,拉贾斯坦邦等上。
为了这个,我已经写了这是工作的罚款一个Ajax功能。
但问题是我不能够在同一时间,这是我在一个时间内获取只有一个国家获取多个状态。

我写了下面的code,以获取国家名称:

HobbyHomeAdress表包含ProvincialStateID我通过一些其他方法来抓取。
然后,我比较于ProvincialState表中ProvincialStateID价值的价值,并获取该表的数据,但在它给我的最后一个记录而已。

 公开的ICollection< ProvincialState> FetchStateByStateid(ICollection的< HobbyHomeAddress> hobbyhomeaddresslist)
    {
        log.Debug(开始);
        ISession的会话= DataAccessLayerHelper.OpenWriterSession();
        ITransaction事务= session.BeginTransaction();
        ICollection的< ProvincialState> provincialstate = NULL;
        尝试
        {
            的foreach(在hobbyhomeaddresslist VAR状态)
            {
                provincialstate = session.CreateCriteria(typeof运算(ProvincialState))
                                。新增(前pression.Eq(ProvincialStateID,state.ProvincialState.ProvincialStateID))
                                .LIST< ProvincialState>();
            }
            器transaction.commit();        }
        赶上(SessionException前)
        {
            如果(交易= NULL&放大器;!&安培; transaction.IsActive)
                transaction.Rollback();            log.Error(除息);
            provincialstate = NULL;
        }
        最后
        {
            如果(交易!= NULL)
                transaction.Dispose();            如果(会话= NULL&放大器;!&安培; session.IsConnected)
                session.Close();            log.Debug(结束);
        }
        返回provincialstate;
    }


解决方案

正在重新创建provincialstate集合在hobbyhomeaddresslist每个状态。所以,你最终用一个单一的入口,通常是最后一个集合。相反,你应该创建集合前期和检索项目后,只需将其添加到该集合。

...喀嚓

  ...
    清单< ProvincialState> provincialstate =新的List< ProvincialState>();
    尝试
    {
        的foreach(在hobbyhomeaddresslist VAR状态)
        {            VAR列表= session.CreateCriteria(typeof运算(ProvincialState))
                            。新增(前pression.Eq(ProvincialStateID,state.ProvincialState.ProvincialStateID))
                            .LIST< ProvincialState>();
            provincialstate.AddRange(名单);
        }
        器transaction.commit();    }
    ...

更新:使用析取单个查询

 的IList< ProvincialState> provincialstate = NULL;
    析取DJ =新脱节();
    尝试
    {
        的foreach(在hobbyhomeaddresslist VAR状态)
        {
            dj.Add(前pression.Eq(ProvincialStateID,state.ProvincialState.ProvincialStateID));
        }
        provincialstate = session.CreateCriteria(typeof运算(ProvincialState))
                       。新增(DJ)
                       .LIST< ProvincialState>();        器transaction.commit();    }

如果你看一下生成的SQL,你现在应该看到一个选择几个where子句中,而不是几个选择用一个where子句。

i am developing an application in mvc3.
I have two dropdowns and on the basis of value selected in first dropdown the second dropdown is populated. The first dropdown is Course and on the basis of course selected the second dropdown populates the states where the course is available.

Foreg.if the course is 'MCA' the states should be Maharashtra,rajasthan and so-on. For this i have written an ajax function which is working fine. But the problem is i am not able to fetch multiple states at a time that is i can fetch only One state at a time.

I have written the following Code to fetch the state name:

HobbyHomeAdress Table contains ProvincialStateID which i fetch through some other method. Then i compare that value with the value in ProvincialStateID in ProvincialState Table and fetch the data of that table but with it gives me the last record only.

 public ICollection<ProvincialState> FetchStateByStateid(ICollection<HobbyHomeAddress> hobbyhomeaddresslist)
    {
        log.Debug("Start");
        ISession session = DataAccessLayerHelper.OpenWriterSession();
        ITransaction transaction = session.BeginTransaction();
        ICollection<ProvincialState> provincialstate = null;
        try
        {
            foreach (var state in hobbyhomeaddresslist)
            {
                provincialstate = session.CreateCriteria(typeof(ProvincialState))
                                .Add(Expression.Eq("ProvincialStateID", state.ProvincialState.ProvincialStateID))
                                .List<ProvincialState>();
            }
            transaction.Commit();

        }
        catch (SessionException ex)
        {
            if (transaction != null && transaction.IsActive)
                transaction.Rollback();

            log.Error(ex);
            provincialstate = null;
        }
        finally
        {
            if (transaction != null)
                transaction.Dispose();

            if (session != null && session.IsConnected)
                session.Close();

            log.Debug("End");
        }
        return provincialstate;
    }

解决方案

you are recreating the provincialstate collection for each state in hobbyhomeaddresslist. So you end up with a collection with a single entry, usually the last one. Instead you should create the collection upfront and after retrieving an item, just add it to that collection.

...snip

    ...
    List<ProvincialState> provincialstate = new List<ProvincialState>();
    try
    {
        foreach (var state in hobbyhomeaddresslist)
        {

            var list = session.CreateCriteria(typeof(ProvincialState))
                            .Add(Expression.Eq("ProvincialStateID",       state.ProvincialState.ProvincialStateID))
                            .List<ProvincialState>();
            provincialstate.AddRange(list);
        }
        transaction.Commit();

    }
    ...

Update: single query using a Disjunction.

    IList<ProvincialState> provincialstate = null;
    Disjunction dj = new Disjunction();    
    try
    {
        foreach (var state in hobbyhomeaddresslist)
        {
            dj.Add(Expression.Eq("ProvincialStateID",       state.ProvincialState.ProvincialStateID));
        }
        provincialstate = session.CreateCriteria(typeof(ProvincialState))
                       .Add(dj)
                       .List<ProvincialState>();

        transaction.Commit();

    }

if you look at the generated SQL, you should now see a single select with several where clauses instead of several selects with a single where clause.

这篇关于如何通过一个ICollection的在MVC3 NHibernate的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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