我正在尝试在执行查询后为“trades.Price_5s”字段分配值。这是代码:
var TradeStatsQuery = (from trades in connection.TradeStatsDatas
where (trades.Price_5s == null) || (trades.Price_10s == null)
|| (trades.Price_30s == null) || (trades.Price_60s == null)
|| (trades.Price_5min == null) || (trades.Price_15min == null)
|| (trades.Price_30min == null) || (trades.Price_60min == null)
select new
{
trades.TradeID,
trades.CurrencyPair,
trades.Action,
trades.ExecutedDateTime,
trades.Price_5s,
trades.Price_10s,
trades.Price_30s,
trades.Price_60s,
trades.Price_5min,
trades.Price_15min,
trades.Price_30min,
trades.Price_60min
});
if(TradeStatsQuery.Count() > 0)
{
foreach(var id in TradeStatsQuery)
{
if(id.Price_5s == null)
{
Console.WriteLine("-----------------------------------------");
Console.WriteLine("Trade ID: " + id.TradeID);
DateTime ET_plus5s = id.ExecutedDateTime.AddSeconds(5);
if(DateTime.Now >= ET_plus5s)
{
decimal retrieved_5sPrice;
Console.WriteLine("Fetching 5 second past trade price...");
retrieved_5sPrice = tb_connection.GetCurrencyRate(id.CurrencyPair, id.Action, ET_plus5s);
Console.WriteLine("Price 5 sec: " + retrieved_5sPrice);
id.Price_5s = retrieved_5sPrice;
我正在尝试将我检索到的价格分配给当前设置为null的查询中的字段,因此我可以稍后返回并覆盖我用其更新值查询的所有字段。这可能吗?我可以为之前查询的字段分配不同的值吗?最后一行是发生错误的地方。
错误消息:“属性或索引器'AnonymousType#1.Price_5s'无法分配给 - 它是只读的”
答案 0 :(得分:1)
在您的linq查询中,您将使用new
创建匿名类型。
根据MS Anonymous Types (C# Programming Guide),这些是只读的。因此,你无法改变他们的价值观。