2017-06-21 7 views
0

私はデータテーブルをメモリに持っており、レコードをいくつか選択してフィールドを変更するレコードを歩かなければならず、同じ変更をデータテーブルに戻します。私はフィルター、ビュー、およびSQLでこれを行うことができますが、私はLinqでそれをやろうとしています。LinqがDataTableとレコードを照会する

 var results = (from rows in dtTheRows.AsEnumerable() 
        select new 
        { 
         rows.Job, 
        }).Distinct(); 


    foreach (var row in results) 
    { 
     firstRow = true; 
     thisOnHand = 0; 

     var here = from thisRow in dtTheRows.AsEnumerable() 
         orderby thisRow.PromisedDate 
         select new 
         { 
          thisRow.OnHandQuantity, 
          thisRow.Balance, 
          thisRow.RemainingQuantity 
         }; 

     foreach(var theRow in here) 
     { 
      // business logic here ... 
      theRow.OnHandQuantity = 5; 

     } // foreach ... 

最初のlinqクエリとforeachは、考慮するデータのサブセットのリストを取得します。関連性がある場合はここに含めます。私の問題は、このラインである:

heRow.OnHandQuantity = 5; 

私のエラーは次のとおりです。 「エラー19施設またはインデクサ 『AnonymousType#1.OnHandQuantityは』に割り当てることはできません - それは読み取り専用です」

を私は何ここに行方不明?このクエリを元のデータテーブルに更新することはできますか?

+0

[C#の匿名型を割り当てることはできません - 読み取り専用です](https://stackoverflow.com/questions/1550797/c-sharp-anonymous-types-cannot-be-assigned-to-それは読み取り専用です) – Igor

答えて

0
var here = from thisRow in dtTheRows.AsEnumerable() 
         orderby thisRow.PromisedDate 
         select new 
         { 
          thisRow.OnHandQuantity, 
          thisRow.Balance, 
          thisRow.RemainingQuantity 
         }; 

の代わりに選択して三つの変数を渡すの

select thisRow; 

、自身thisRow渡します。それは文のエラーを解決するかもしれません - theRow.OnHandQuantity = 5;

0

エラーは自己記述的です。匿名型は更新/変更できません。クエリから変更したい元のエンティティを返さなければなりません。代わりに

select new 
{ 
    thisRow.OnHandQuantity, 
    thisRow.Balance, 
    thisRow.RemainingQuantity 
}; 
関連する問題