how to iterate through an Icollection In MVC3 nhib

2019-09-09 04:28发布

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条回答
forever°为你锁心
2楼-- · 2019-09-09 04:38

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.

查看更多
登录 后发表回答