示例#1
0
        private Product LoadProductWithId(ITransaction transaction, int id)
        {
            var query = new SelectionQuery()
                        .From(QueryFrom.EntityType(typeof(Product)))
                        .Where(QueryCondition.Expression(ConditionExpr.Build().Field(typeof(Product), "ProductId").Eq().Value(id)))
                        .Select(QuerySelection.EntityType(typeof(Product))).ToList(transaction);

            return(query.First() as Product);
        }
        private IList <IEntity> QueryTest(IList <IEntity> items)
        {
            var newList = new List <IEntity>();

            var sw = new Stopwatch();

            sw.Start();
            var tx = transactionFactory.CreateTransaction();

            for (int i = 0; i < items.Count; i++)
            {
                var item     = items[i];
                var itemType = item.GetType();

                if (itemType == typeof(Transaction))
                {
                    var trns   = (Transaction)item;
                    var loaded = new SelectionQuery()
                                 .From(QueryFrom.EntityType(item.GetType()))
                                 .Select(QuerySelection.EntityType(item.GetType()))
                                 .Where(QueryCondition.Expression(ConditionExpr.Build().Field(itemType, "TransactionId").Eq().Value(trns.TransactionId)))
                                 .ToList(tx).FirstOrDefault();
                    newList.Add((IEntity)loaded);
                }
                else if (itemType == typeof(Service))
                {
                    var svc    = (Service)item;
                    var loaded = new SelectionQuery()
                                 .From(QueryFrom.EntityType(item.GetType()))
                                 .Select(QuerySelection.EntityType(item.GetType()))
                                 .Where(QueryCondition.Expression(ConditionExpr.Build().Field(itemType, "ItemId").Eq().Value(svc.ItemId)))
                                 .ToList(tx).FirstOrDefault();
                    newList.Add((IEntity)loaded);
                }
                else if (itemType == typeof(Product))
                {
                    var product = (Product)item;
                    var loaded  = new SelectionQuery()
                                  .From(QueryFrom.EntityType(item.GetType()))
                                  .Select(QuerySelection.EntityType(item.GetType()))
                                  .Where(QueryCondition.Expression(ConditionExpr.Build().Field(itemType, "ItemId").Eq().Value(product.ItemId)))
                                  .ToList(tx).FirstOrDefault();
                    newList.Add((IEntity)loaded);
                }

                if (i % 100 == 0)
                {
                    tx.Close();
                    tx = transactionFactory.CreateTransaction();
                }
            }
            tx.Commit();
            tx.Close();
            sw.Stop();

            var speed = items.Count * 1000 / sw.ElapsedMilliseconds;

            LoggerManager.GetLogger(Assembly.GetExecutingAssembly(), typeof(NDbGatePerformanceCounter))
            .Log(typeof(NDbGatePerformanceCounter), Level.Warn, $"NDBGate Thread Query speed  {speed} entities/second", null);
            return(newList);
        }