2016-04-10 26 views
0

Bu, bir tablo oluşturduğum koddur. Bu kod tüm satırları döndürür ama ürünün fiyat büyüktür 6.Veri kümesinden belirli satırlar nasıl okunur

class Program 
{ 
    static void Main(string[] args) 
    { 
     DataTable MyTable = GetTable(); // Get the data table. 

     foreach (DataRow row in MyTable.Rows) // Loop over the rows. 
     { 
      Console.WriteLine("--- Row ---"); 
      foreach (var item in row.ItemArray) // Loop over the items. 
      { 
       Console.Write("Item: "); 
       Console.WriteLine(item); 
      } 
     } 

     Console.Read(); // Pause. 
    } 

    static DataTable GetTable() 
    { 
     DataTable table = new DataTable(); 
     table.Columns.Add("Price", typeof(int)); 
     table.Columns.Add("Name", typeof(string)); 

     table.Rows.Add(15, "Bag"); 
     table.Rows.Add(4, "Candies"); 
     table.Rows.Add(2, "Cookies"); 
     table.Rows.Add(20, "Books"); 
     table.Rows.Add(8, "Chocolates"); 
     return table; 
    } 
    } 
} 

cevap

0

Basitçe bu gibi gösteren önce Fiyatı kontrol nerede olduğunu yalnızca belirli satır istiyorum:

foreach (DataRow row in MyTable.Rows) // Loop over the rows. 
 
     { 
 
      Console.WriteLine("--- Row ---"); 
 
      if (row["Price"] > 6) 
 
      { 
 
       foreach (var item in row.ItemArray) // Loop over the items. 
 
       { 
 
        Console.Write("Item: "); 
 
        Console.WriteLine(item); 
 
       } 
 
      } 
 
     }

DÜZENLEME: Eksik bir son paragraf ekledim!

+0

Zaten denedim ama çalışmıyor –

+0

Denediğinizde ne olur? Bir istisna mı atıyor yoksa ne? –

+0

basitçe tüm çıktılar özel satırları göstermiyor –

İlgili konular