我正在使用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变为"Cancelled"状态之前,一切都很正常。我想这是因为我在查询的顶部过滤了取消的订单,但我不知道这有什么关系。
我终于找到了解决问题的方法。事实证明,Obtics在使用lambda创建动态类方面存在问题,例如
from o in orders where o.state == "active" select new OrderModel2 {Underlying = o}
新关键字把一切都搞砸了。您需要手动处理这个类的创建。
不管怎样,经过两天的努力,我终于有了这么多代码。
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
};
}
它很管用,但看起来很难看,如果有人能让它更漂亮,我会很感激的。
问题不在于'new'关键字,而在于OrderModel2类型的两个实例不相等,即使它们是用相同的构造参数创建的。
这可以通过三种方式解决:
- 重写对象的Equals方法
- 将ObticsQualityComparerAttribute应用于具有特定相等比较器的类,以供Obtics使用
- 创建一个具有存储库的工厂,该存储库在使用相同的构造参数调用时返回相同的对象实例
您似乎使用了数字3,不过我认为您的代码可能更简单、更通用。