0
 public bool SaveValidTicketNos(string id,string[] ticketNos, string checkType, string checkMan)
        {
            bool result = false;
            List<Carstartlistticket>enties=new List<Carstartlistticket>();
            using (var context = new MiniSysDataContext())
            {
                try
                {
                    foreach (var ticketNo in ticketNos)
                    {
                        Orderticket temp = context.Orderticket.ByTicketNo(ticketNo).SingleOrDefault();
                        if (temp != null)
                        {
                            Ticketline ticketline= temp.Ticketline;
                            string currencyType = temp.CurrencyType;
                            float personAllowance=GetPersonCountAllowance(context,ticketline, currencyType);
                            Carstartlistticket carstartlistticket = new Carstartlistticket()
                                                                        {
                                                                            CsltId = Guid.NewGuid().ToString(),
                                                                            Carstartlist = new Carstartlist(){CslId = id},
                                                                            LeaveDate = temp.LeaveDate,
                                                                            OnPointName = temp.OnpointName,
                                                                            OffPointName = temp.OffpointName,
                                                                            OutTicketMan = temp.OutBy,
                                                                            TicketNo = temp.TicketNo,
                                                                            ChekMan = checkMan,
                                                                            Type = string.IsNullOrEmpty(checkType)?(short?)null:Convert.ToInt16(checkType),
                                                                            CreatedOn = DateTime.Now,
                                                                            CreatedBy = checkMan,
                                                                            NumbserAllowance = personAllowance
                                                                        };
                            enties.Add(carstartlistticket);
                        }
                    }

                    context.BeginTransaction();
                    context.Carstartlistticket.InsertAllOnSubmit(enties);
                    context.SubmitChanges();
                    bool changeStateResult=ChangeTicketState(context, ticketNos,checkMan);
                    if(changeStateResult)
                    {
                        context.CommitTransaction();
                        result = true;
                    }
                    else
                    {
                        context.RollbackTransaction();
                    }
                }
                catch (Exception e)
                {
                    LogHelper.WriteLog(string.Format("CarstartlistService.SaveValidTicketNos({0},{1},{2},{3})",id,ticketNos,checkType,checkMan),e);
                    context.RollbackTransaction();
                }
            }
            return result;
        }

我的代码在上面。我怀疑这些代码的性能很差。在这一点上表现不佳

Orderticket temp = context.Orderticket.ByTicketNo(ticketNo).SingleOrDefault();

,实际上,我通过args方法得到一个字符串数组,然后我想通过ticketNos从数据库中获取所有数据,这里我使用一个循环,我知道如果我这样写我的代码会导致性能问题,它会再引导一次数据库访问,如何避免这个问题并提高代码性能,例如,仅通过数据库访问获取所有数据我忘了告诉你我使用的 ORM,恩,ORM 是基于 PlinqO 的 NHibernate

我期待着你的每一个答案,谢谢

4

1 回答 1

0

使用普通的 NHibernate

var tickets = session.QueryOver<OrderTicket>()
    .WhereRestrictionOn(x => x.TicketNo).IsIn(ticketNos)
    .List();

short? type = null;
short typeValue;
if (!string.IsNullOrEmpty(checkType) && short.TryParse(checkType, out typeValue))
    type = typeValue;

var entitiesToSave = tickets.Select(ticket => new Carstartlistticket
{
    CsltId = Guid.NewGuid().ToString(),
    Carstartlist = new Carstartlist() { CslId = id },
    LeaveDate = ticket.LeaveDate,
    OnPointName = ticket.OnpointName,
    OffPointName = ticket.OffpointName,
    OutTicketMan = ticket.OutBy,
    TicketNo = ticket.TicketNo,
    ChekMan = checkMan,
    CreatedOn = DateTime.Now,
    CreatedBy = checkMan,
    Type = type,
    NumbserAllowance = GetPersonCountAllowance(context, ticket.Ticketline, ticket.CurrencyType)
});

foreach (var entity in entitiesToSave)
{
    session.Save(entity);
}

为了进一步增强这一点,请尝试预加载所有需要的 PersonCountAllowances

于 2013-07-31T07:47:06.727 回答