2015-03-18 55 views
0

我一直在寻找一些方法来使用DataTables并使用自定义类获取她的行的值。典型的DataTable使用自定义类获取她的行值

我的做法是这样的:

DataTable dtUsers = new DataTable(); 

dtUsers.Columns.Add("Name"); 
dtUsers.Columns.Add("Last name"); 
dtUsers.Columns.Add("Age", typeof(int)); 

for (int x = 1; x <= 100; x++) 
{ 
    dtUsers.Rows.Add(new object[] 
    { 
     "Name " + x, 
     "Last name " + x, 
     x 
    }); 
} 

// I want to do this 
var Name = dtUsers.Rows[0].Name; // Get "Name 1" 
var Age = dtUsers.Rows[50].Age; // Get "Age 50" 

// Or 
foreach(DataRow drCurrent in dtUsers.Select("Age > 50")) 
{ 
    int Age = drCurrent.Age; // Get 51,52,53.. 
} 

我想是这样的:

public class DataTablePerson : DataTable 
{ 
    public string Name { get; set; } // Implement a method to return a specific value in some specific row 
    public string Last_name { get; set; } // Implement a method to return a specific value in some specific row 
    public int Age { get; set; }// Implement a method to return a specific value in some specific row 
} 

的存在是为了实现这一目标的方法?

如果我可以使用自定义类作为参数some typized class来重新应用这个类的其他结构(例如:Person,Tree,Movie,Book),那么这将是有用的。

回答

1

错误否,请勿为您的应用程序使用基于数组的数据模型。 I have blogged it why。相反,将数据表转换为您自己的班级模型(POCO)的List/Array/Enumerable并改为使用它。

但是,如果你坚持使用它(虽然是你自己的选择),你可以用“包装类”来做到这一点。

public class DataTablePerson{ 
    public DataTablePerson(DataTable source){ 
     this.Source = source; 
    } 
    protected DataTable Source = null; 

    public string Name(int row = 0){ 
     return Get("name", row); 
    } 
    public string LastName(int row = 0){ 
     return Get("last_name", row); 
    } 
    public int Age(int row = 0){ 
     return Convert.ToInt32(Get("age", row)); 
    } 

    protected string Get(string name, int row){ 
     return source.Rows[row][name].ToString(); 
    } 
} 

而且使用它像:

DataTablePerson dtp = new DataTablePerson(dtUsers); 
string name = dtp.Name(1); 

或者,如果你想要行级实体(可以换取虽然使用属性):

public class DataRowPerson{ 
    public DataRowPerson(DataRow source){ 
     this.Source = source; 
    } 
    protected DataRow Source = null; 

    public string Name(){ 
     return source["name"].ToString(); 
    } 
    public string LastName(){ 
     return source["last_name"].ToString(); 
    } 
    public int Age(){ 
     return Convert.ToInt32(source["age"].ToString())); 
    } 
} 

而且使用它像:

foreach(DataRow drCurrent in dtUsers.Select("Age > 50")) 
{ 
    DataRowPerson drp = new DataRowPerson(drCurrent); 
    int Age = drp.Age; // Get 51,52,53.. 
} 
0

你差点没钱了!唯一的不同,使用属性的符号,你只需要为方括号中的字符串来指定列名:即使使用LINQ

var agesOverFifty = new List<int>(); 
foreach (DataRow currentRow in dtUsers.Select("Age > 50")) 
{ 
    agesOverFifty.Add(Convert.ToInt32(currentRow["Age"])); 
} 

或者:

var Name = dtUsers.Rows[0]["Name"]; 
var Age = dtUsers.Rows[50]["Age"]; 

或者

List<int> agesOverFifty = dtUsers.Select("Age > 50") 
    .Select(row => Convert.ToInt32(row["Age"])) 
    .ToList(); 

Console.WriteLine("The average age of a person over 50 in our sample is: {0}", 
    agesOverFifty.Average()); 
+0

谢谢,但; 1)这是正常的方法(我想知道源中的现有列)。 2和3)我会得到一个特定列的值;我想拥有DataRow的所有值。 – MiBol 2015-03-19 01:29:11

相关问题