0

我正在使用 Obtics 库进行实时 linq 查询。

但是我无法通过这个完全没有意义的奇怪异常。

这是我的查询:

var query = ExpressionObserver.Execute(() =>
        from o in _orders.DefaultIfEmpty(new OrderStatusViewModel())
        where o.State != OrderStateEnum.Canceled
        group o by o.Isin
            into g
            let name = _referenceData.GetInstrumentName(g.Key)
            orderby name ascending
            select new ComplexRowViewModel(_referenceData)
            {
                UnderlyingOrder = g.First(),
                PrimaryExchange = (from q in _quotes.DefaultIfEmpty(new QuoteTickViewModel()).Where(w => w.Exchange == _referenceData.GetPrimaryExchangeId(g.Key) && w.Isin == g.Key && w.Provider == ProviderEnum.Bloomberg)
                                   select new SimpleRowViewModel()
                                   {
                                       UnderlyingQuote = q
                                   }).First(),
                Groupped = (from y in _orders.DefaultIfEmpty(new OrderStatusViewModel())
                            join x in _quotes.DefaultIfEmpty(new QuoteTickViewModel()) on new { y.Isin, y.Exchange }
                                equals new { x.Isin, x.Exchange }
                            where
                                y.Isin == g.Key &&
                                y.State != OrderStateEnum.Canceled &&
                                x.Provider == ProviderEnum.Tradebase &&
                                x.Exchange !=
                                _referenceData.GetPrimaryExchangeId(g.Key)
                            group x by new { x.Exchange }
                                into p
                                select new SimpleRowViewModel()
                                {
                                    UnderlyingQuote = p.First()
                                }
                           ),
                Uncompressed = (from o in _orders.DefaultIfEmpty(new OrderStatusViewModel())
                                where o.State != OrderStateEnum.Canceled && o.Isin == g.Key
                                select new UncompressedRowViewModel() { UnderlyingOrder = o }),

                Compressed = (from o in _orders.DefaultIfEmpty(new OrderStatusViewModel())
                              where o.State != OrderStateEnum.Canceled && o.Isin == g.Key
                              group o by new { o.LimitPrice, o.OrderSide } into x
                              select new CompressedRowViewModel()
                              {
                                  Ask = x.Key.OrderSide == OrderSideEnum.Sell ? (decimal?)x.Key.LimitPrice : (decimal?)null,
                                  AskSize = x.Key.OrderSide == OrderSideEnum.Sell ? x.Select(s => s.Quantity).Aggregate((c, n) => c + n) : 0,
                                  Bid = x.Key.OrderSide == OrderSideEnum.Buy ? (decimal?)x.Key.LimitPrice : (decimal?)null,
                                  BidSize = x.Key.OrderSide == OrderSideEnum.Buy ? x.Select(s => s.Quantity).Aggregate((c, n) => c + n) : 0,
                                  Exchange = string.Join(", ", x.Select(s => s.Exchange)),
                                  Isin = x.First().Isin.ToString(),
                                  OrderBuyCount = x.Key.OrderSide == OrderSideEnum.Buy ? x.Count() : 0,
                                  OrderSellCount = x.Key.OrderSide == OrderSideEnum.Sell ? x.Count() : 0,
                                  RowSide = x.Key.OrderSide == OrderSideEnum.Sell ? RowSide.Sell : RowSide.Buy
                              })}
    ).Cascade();

GridData = query;

我上传了使这一切成为可能的课程。http://www.4shared.com/file/ce_V8PPh/MarketData.html

例外是:

InvalidOperationException, Added item does not appear at given index '0'.

但这没有任何意义,因为该项目已经存在。

一切正常,直到 OrderStatus 进入“已取消”状态。我认为这是因为我在查询之上过滤了取消的订单,但我不知道这有什么关系。

4

2 回答 2

0

我终于找到了解决我的问题的方法。事实证明,Obtics 在使用 lambda 创建动态类时存在问题,例如

from o in orders where o.state == "active" select new OrderModel2 {Underlying = o}

新关键字搞砸了。您需要手动处理此类创建。

无论如何,经过 2 天的头部粉碎后,我最终得到了这么多代码。

private void CreateQueries()
{
    var query = ExpressionObserver.Execute(() => (from o in _orders
                                                  where o.State != OrderStateEnum.Canceled
                                                  group o by o.Isin
                                                      into g
                                                      let name = _referenceData.GetInstrumentName(g.Key)
                                                      orderby name ascending
                                                      select GetComplexRowViewModel(
                                                                                    g.First()
                                                                                    ,
                                                                                    (from p in _quotes
                                                                                     where
                                                                                         p.Isin == g.Key &&
                                                                                         p.Exchange == _referenceData.GetPrimaryExchangeId(g.Key) &&
                                                                                         p.Provider == ProviderEnum.Bloomberg
                                                                                     select GetSimpleRowViewModel(p))
                                                                                     ,
                                                                                     (from q in _quotes
                                                                                      where
                                                                                          q.Isin == g.Key &&
                                                                                          q.Provider == ProviderEnum.Tradebase &&
                                                                                          g.Select(s => s.Exchange).Contains(q.Exchange)
                                                                                      select GetSimpleRowViewModel(q))
                                                                                      ,
                                                                                      (from o in _orders
                                                                                       where o.Isin == g.Key
                                                                                       && o.State != OrderStateEnum.Canceled
                                                                                       group o by new { o.LimitPrice, o.OrderSide } into x
                                                                                       select GetCompressedRowViewModel
                                                                                       (
                                                                                           x.Key.OrderSide == OrderSideEnum.Sell ? (decimal?)x.Key.LimitPrice : (decimal?)null,
                                                                                           x.Key.OrderSide == OrderSideEnum.Sell ? x.Select(s => s.Quantity).Aggregate((c, n) => c + n) : 0,
                                                                                           x.Key.OrderSide == OrderSideEnum.Buy ? (decimal?)x.Key.LimitPrice : (decimal?)null,
                                                                                           x.Key.OrderSide == OrderSideEnum.Buy ? x.Select(s => s.Quantity).Aggregate((c, n) => c + n) : 0,
                                                                                           string.Join(", ", x.Select(s => s.Exchange)),
                                                                                           x.First().Isin.ToString(),
                                                                                           x.Key.OrderSide == OrderSideEnum.Buy ? x.Count() : 0,
                                                                                           x.Key.OrderSide == OrderSideEnum.Sell ? x.Count() : 0,
                                                                                           x.Key.OrderSide == OrderSideEnum.Sell ? RowSide.Sell : RowSide.Buy
                                                                                       ))
                                                           ,
                                                           (from o in _orders
                                                            where o.Isin == g.Key
                                                            && o.State != OrderStateEnum.Canceled
                                                            select GetUncompressedRowViewModel(o))

                                                                                     ))).Cascade();
    GridData = query;
}


// Obtics 
private ConcurrentDictionary<string, ComplexRowViewModel> _complexRowViewModels = new ConcurrentDictionary<string, ComplexRowViewModel>();
private ComplexRowViewModel GetComplexRowViewModel(OrderStatusViewModel model, IEnumerable<SimpleRowViewModel> primaryExchanges, IEnumerable<SimpleRowViewModel> groupped
    , IEnumerable<CompressedRowViewModel> compressed, IEnumerable<UncompressedRowViewModel> uncompressed)
{
    if (model == null)
        return _complexRowViewModels.GetOrAdd("", s2 => new ComplexRowViewModel());

    return _complexRowViewModels.GetOrAdd(model.Isin,
                                          s2 =>
                                          new ComplexRowViewModel(_referenceData) { UnderlyingOrder = model, PrimaryExchange = primaryExchanges.DefaultIfEmpty(new SimpleRowViewModel()).First(), Groupped = groupped, Compressed = compressed, Uncompressed = uncompressed });
}

private ConcurrentDictionary<string, SimpleRowViewModel> _simpleRowViewModels = new ConcurrentDictionary<string, SimpleRowViewModel>();
private SimpleRowViewModel GetSimpleRowViewModel(QuoteTickViewModel model)
{
    if (model == null)
        return _simpleRowViewModels.GetOrAdd("", s2 => new SimpleRowViewModel());

    return _simpleRowViewModels.GetOrAdd(model.Isin + model.Exchange,
                                                        s2 =>
                                                        new SimpleRowViewModel() { UnderlyingQuote = model });
}

private ConcurrentDictionary<string, UncompressedRowViewModel> _uncompressedRowViewModels = new ConcurrentDictionary<string, UncompressedRowViewModel>();
private UncompressedRowViewModel GetUncompressedRowViewModel(OrderStatusViewModel model)
{
    if (model == null)
        return _uncompressedRowViewModels.GetOrAdd("", s2 => new UncompressedRowViewModel());

    return _uncompressedRowViewModels.GetOrAdd(model.InternalId,
                                                        s2 =>
                                                        new UncompressedRowViewModel() { UnderlyingOrder = model });
}

private ConcurrentDictionary<string, CompressedRowViewModel> _compressedRowViewModels = new ConcurrentDictionary<string, CompressedRowViewModel>();
private CompressedRowViewModel GetCompressedRowViewModel(decimal? Ask, int AskSize, decimal? Bid, int BidSize, string Exchange, string Isin, int OrderBuyCount, int OrderSellCount, RowSide RowSide)
{

    return new CompressedRowViewModel()
               {
                   Ask = Ask,
                   AskSize = AskSize,
                   Bid = Bid,
                   BidSize = BidSize,
                   Exchange = Exchange,
                   Isin = Isin,
                   OrderBuyCount = OrderBuyCount,
                   OrderSellCount = OrderSellCount,
                   RowSide = RowSide
               };
}

它有效,但看起来很丑,如果有人有办法让它更漂亮,我将不胜感激。

于 2012-07-11T08:25:57.543 回答
0

问题不在于“new”关键字,而是 OrderModel2 类型的两个实例不相等,即使它们是使用相等的构造参数创建的。

这可以通过三种方式解决:

  1. 覆盖对象的 Equals 方法。
  2. 使用特定的相等比较器将 ObticsEqualityComparerAttribute 应用于您的类以供 Obtics 使用。
  3. 创建具有存储库的工厂,该存储库在使用相同的构造参数调用时返回相同的对象实例。

您似乎使用了数字 3,但我认为您的代码可能更简单和通用。

于 2012-12-19T09:47:17.107 回答