how to iterate through an Icollection In MVC3 nhibernate

梦想的初衷 提交于 2020-01-06 14:21:29

问题


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;
    }

回答1:


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.



来源:https://stackoverflow.com/questions/10136094/how-to-iterate-through-an-icollection-in-mvc3-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!