2017-10-04 12 views
0

私はこのプログラムに.txtファイルを読み込み、List<Class>を作成します。このクラスには10個の "列"があります。参照トータルUOM列enter image description here余分な計算列を作成するリストの操作

下の画像は、.txtファイルではありませんが、私は私のコレクションに追加の列としてこの余分な列を作成したいと思います。したがって、テキストファイルの最後の列は「幅」です。次のように

計算すべきである:幅= 0は、合計UOMは数量*の長さでなければならない行について

(正味重量を無視)

幅> 0は、合計UOMは数量でなければならない行について*これまでの長さ*幅

コード:

// Structure of SteelAssembly Class object 
    public class SteelAssembly 
    { 
     public int LotNo { get; set; } 
     public string AssemblyMark { get; set; } 
     public string AssemblyName { get; set; } 
     public string Part { get; set; } 
     public int Quantity { get; set; } 
     public string Profile { get; set; } 
     public string Grade { get; set; } 
     public decimal Length { get; set; } 
     public decimal Weight { get; set; } 
     public decimal Width { get; set; } 
    } 

...............

lines = new List<string>(); 
         assembly = new List<SteelAssembly>(); 

         // Open the text file using a stream reader. 
         using (StreamReader sr = new StreamReader(openFileDialog.FileName)) 
         { 
          // Read the stream to a string, and write the string to the console. 
          while (sr.Peek() >= 0) 
           lines.Add(sr.ReadLine()); 
         } 

         // Read job number 
         jobNumber = lines.FirstOrDefault(); 

         // Remove job number from list 
         lines.Remove(jobNumber); 
         if (lines.Count > 0) 
         { 
          try 
          { 
           foreach (string row in lines) 
           { 
            // Split the columns of the .txt document 
            string[] words = row.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); 

            // Remove all null or empty rows 
            if (words != null || words.Length != 0) 
            { 
             // Transform data to SteelAssembly Class object 
             assembly.Add(new SteelAssembly 
             { 
              LotNo = Convert.ToInt32(words[0]), 
              AssemblyMark = words[1].Trim(), 
              AssemblyName = words[2].Trim(), 
              Part = words[3].Trim(), 
              Quantity = Convert.ToInt32(words[4]), 
              Profile = words[5].Trim(), 
              Grade = words[6].Trim(), 
              Length = Convert.ToDecimal(words[7]), 
              Weight = Convert.ToDecimal(words[8]), 
              Width = Convert.ToDecimal(words[9]), 
             }); 
            } 
           } 
          } 

....................

我々はこの辞書

を持っているように、各独特のアセンブリマーク/ロット番号は、新しい製造指図になります

私は正しい合計UOMと私の辞書に余分な列を取得するために管理するにはどうすればよい

enter image description here

// Create dictionary   
var dictionary = assembly 
       .GroupBy(x => new 
       { 
        x.AssemblyMark, 
        x.LotNo         
       }) 
       .ToDictionary(x => x.Key, x => x.ToList()); 

これは変身しますか?

答えて

1

getこれらの基準に基づいて計算された値を返すプロパティのみを作成してください。このプロパティの値は、あなたがそれを読んでいるときにのみ計算されていることを

public class SteelAssembly 
{ 
    public int LotNo { get; set; } 
    public string AssemblyMark { get; set; } 
    public string AssemblyName { get; set; } 
    public string Part { get; set; } 
    public int Quantity { get; set; } 
    public string Profile { get; set; } 
    public string Grade { get; set; } 
    public decimal Length { get; set; } 
    public decimal Weight { get; set; } 
    public decimal Width { get; set; } 

    public decimal UoM 
    { 
     get 
     { 
      return Width == 0 ? (Quantity * Length) : (Quantity * Length * Width); 
     } 
    } 
} 

注:このような何かは十分なはずです。

+0

この値を設定してアセンブリリストを作成する方法 –

+0

'Width'、' Quantity'などを入力すると自動的に計算されます。他のプロパティが設定されたらアイテムから読み込みます。 – Nisarg

+0

私は参照してください。これは問題ありません。 –

関連する問題