怪异的Obtics例外

时间:2012-07-09 14:22:47

标签: wpf linq mvvm viewmodel inotifypropertychanged

我正在使用Obtics库进行live-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获得“已取消”状态之前,一切正常。我认为这是因为我在查询之上过滤了已取消的订单,但这与我不知道如何相关。

2 个答案:

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

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

答案 1 :(得分:0)

问题不在于&#39; new&#39;关键字,但两个类型为OrderModel2的实例不相等,即使它们是使用相同的构造参数创建的。

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

  1. 覆盖对象的Equals方法。
  2. 将ObticsEqualityComparerAttribute应用于您的类,并使用特定的相等比较器供Obtics使用。
  3. 创建一个带有存储库的工厂,该存储库在使用相同的构造参数调用时返回相同的对象实例。
  4. 您似乎使用了3号,但我认为您的代码可能更简单和通用。