2013-05-08 48 views
-3

如何使用ClientID從Ilist中搜索客戶端名稱。我無法弄清楚。我創建了一個名爲clientdetails的簡單類,並與oracle數據庫建立了連接。現在我只想使用ClientID搜索列表並顯示結果。如何搜索Ilist中的項目並顯示輸出結果?

謝謝AK。

class ClientDetails 
{ 
    public string ClientID; 
    public string ClientName; 
    public string CreatedBy; 
    public string UpdatedBy; 

    public ClientDetails(string ClientID, string ClientName, string CreatedBy, string UpdatedBy) 
    { 
     this.ClientID = ClientID; 
     this.ClientName = ClientName; 
     this.CreatedBy = CreatedBy; 
     this.UpdatedBy = UpdatedBy; 
    } 
} 
class ConnectionSample 
{ 
    static void Main() 
    { 
     OracleConnection con = new OracleConnection(); 

     //using connection string to connect to oracle database 
     IList<ClientDetails> myfield = new List<ClientDetails>(); 
     try 
     { 

      con.ConnectionString = "xxxxxconnection stringxxxxx"; 
      con.Open(); 
      OracleCommand command = con.CreateCommand(); 
      string abc = "SELECT * FROM CLI_CLIENT_900"; 
      command.CommandText = abc; 
      OracleDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       myfield.Add(new ClientDetails(reader["CLIENT_ID"].ToString(), reader["CLIENT_NAME"].ToString(), reader["CREATED_BY"].ToString(), reader["UPDATED_BY"].ToString())); 

      } 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Error" + ex, "Error"); 
     } 
     //close and dispose oracleconnection object 

     con.Close(); 
     con.Dispose(); 

     foreach (ClientDetails c1 in myfield.OrderByDescending(s => s.ClientID)) 
     { 

      Console.Write("\n" + c1.ClientID); 
      Console.Write("\t"+c1.ClientName); 
      Console.Write("\t\t"+c1.UpdatedBy); 
     } 
    } 
    static void Display(IList<string> myfield) 
    { 
     foreach (string value in myfield) 
     { 
      Console.WriteLine("\t"+value); 
     } 
    } 
+2

爲什麼你不在*數據庫*中執行過濾?你真正的*問題*是什麼? – 2013-05-08 22:05:49

+0

我想使用clientid在列表上執行搜索操作。 – AK1 2013-05-08 22:07:15

+1

@AbeMiessler *因此*在這裏沒問題,如果OP的意思是「如何搜索一個項目並且_thereby_顯示它」 – poolie 2013-05-08 22:07:25

回答

4

如果你有一個項目List您可以選擇下列方式具體項目:

myfield.Where(t => t.ClientId == 1234); 

這將返回ClientDetails對象的集合,其中ClientId成員等於1234

也就是說,這可能是過濾,你會更好的在數據庫中做,除非你有一個令人信服的理由不會。

+0

感謝您的答覆@Abe Miessler – AK1 2013-05-08 22:09:07

+0

糟糕! '=!= ==' – demoncodemonkey 2013-05-08 22:11:13

1

您可以使用First/FirstOrDefault

ClientDetails client = myfield.FirstOrDefault(c => c.clientID == givenID); 
if(client != null) 
    Console.Write(client.ClientName); 
else 
    Console.Write("clientID not found"); 
1

如果你有代碼,您可以使用一個簡單的LINQ查詢以獲得所需結果的列表。

var results = myList.Where(x => x.ClientId == clientIdImSearchingFor); 

foreach (Client c in results) 
    // print client data 

你也可以讓它成爲一個循環,如果它在裏面;

foreach (Client c in ClientsList) 
    { 
     if (c.ClientId == ClientIdImSearchingFor) 
      //print client data 
    } 

另外,如果你想與您的SQL查詢(而不是序列化後的結果),你可以使用相同的LINQ語句只MYLIST反而會是它代表了表的實體進行過濾。

+0

糟糕! '=!= ==' – demoncodemonkey 2013-05-08 22:09:29

+1

@demoncodemonkey固定。 – evanmcdonnal 2013-05-08 22:11:07

+0

@Abe犯了同樣的錯誤;) – demoncodemonkey 2013-05-08 22:11:39