2015-06-15 128 views
0

我想根據傳入我的方法的查詢創建一個列表。我的問題是確定如何將這些項目添加到我作爲結果列表返回的列表中。以下是包含了我的名單,什麼將有希望我填充該列表的方式代碼...創建數據庫對象列表

public void QueryInto<T>(ref List<T> returnType, string queryString, string[] parameters = null) 
{  
    try 
    { 
     // Setup the query. 
     SetupCommand(queryString); 

     // Setup the parameters. 
     AddParameters(parameters); 

     // Execute the reader. 
     OpenReader(); 


     // Make sure the statement returns rows... 
     if (reader.HasRows) 
     { 
      // For each row, do this... 
      while (reader.Read()) 
      { 
       // TODO: use datamapping to map to model and add items to list... 

      } 

     } 

    } 

或許有這樣做的更好的辦法,但到目前爲止,這是谷歌已指示的方向我!

+0

reader.HasRows不nessecary,如果沒有別的比while循環發生的內幕,如果塊,因爲如果沒有行,該代碼將永遠不會while循環中得到 – LInsoDeTeh

+0

你檢查'動態LINQ '? – Stefan

+1

你可以使用'Dapper'來對你的db執行sql並返回一個強類型的集合。 – Ric

回答

0

您可以使用Dapper

示例用途;

public class Dog 
{ 
    public int? Age { get; set; } 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public float? Weight { get; set; } 
}    

var guid = Guid.NewGuid(); 
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });