wpfc#LINQ:为检索到的查询中的字段分配不同的值

wpfc#LINQ:为检索到的查询中的字段分配不同的值,c#,sql,wpf,linq,C#,Sql,Wpf,Linq,我试图在执行查询后为“trades.Price_5s”字段赋值。代码如下: var TradeStatsQuery = (from trades in connection.TradeStatsDatas where (trades.Price_5s == null) || (trades.Price_10s == null) || (trades.Price_30s ==

我试图在执行查询后为“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'分配给--它是只读的”

在linq查询中,您正在使用新的
创建匿名类型


根据MS,这些是只读的。因此,您无法更改它们的值。

您尝试过吗?如果是这样,会发生什么呢?而且您不需要
if(tradestatquery.Count()>0)
。因为如果不返回任何内容,则集合将为空,因此,
foreach
无论如何都不会执行任何操作。是的,我收到的错误表示以下“属性或索引器'AnonymousType#1.Price_5s'无法分配到--它是只读的”所以我猜这意味着我无法正确更改它lol没有任何可用的解决方法@Peter MOh好的谢谢我不知道@PeterM@kknaguib或者创建一个新对象而不是对其进行变异,或者更好地从查询本身分配正确的值,而不是稍后尝试对其进行变异。好的,那么我如何使用另一种方法解决这个问题呢。因此,我在一段时间后检索市场价格,最后我希望将其加载到数据库中,只更改具有空值的字段@
select new{}
的Peter MInstead创建一个具有这些属性的类,然后尝试类似于
select new MyContainer{}
的方法,我可以在1099的基础上以体面的$费率进行咨询:D,但如果没有,听起来你必须做一些事情,比如建立一个不符合5s标准的股票列表,然后重新运行linq查询。哈哈,我会考虑一下这个咨询:D好的,我会考虑一下,看看我能想出什么谢谢@Peter m