如何才能提高这个Linq查询表达式的性能?

时间:2013-07-31 03:15:44

标签: performance linq nhibernate plinqo

 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,en,ORM是基于PlinqO的NHibernate

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

1 个答案:

答案 0 :(得分: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