我想获取列表类的属性。我如何通过不循环获取价值?因为我希望列表成为数组。因为目前在第二种情况下,它正在使用索引,是否有一种方法可以像第一种情况下那样简单?
之前,Iam创建了一个仅包含1个属性的列表,并且该列表起作用了。此外,我想通过使用类/模型使其变得有点复杂。但是当我尝试按班使用时,就无法期望我的期望。
//Model
public class model
{
private string mCounter;
private string mName;
private string mEmail;
public model(string mCounter,string mName,string mEmail)
{
this.mCounter = mCounter;
this.mName = mName;
this.mEmail = mEmail;
}
public string get_Counter
{
get { return mCounter; }
}
public string get_Name
{
get { return mName; }
}
public string get_Email
{
get { return mEmail; }
}
}
//Example Data
DataTable dt = new DataTable();
dt.Columns.Add("counter", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("e-mail", typeof(string));
dt.Rows.Add("1", "David","David@gmail.com");
dt.Rows.Add("2", "Ben","Ben@gmail.com");
dt.Rows.Add("3", "Henry","Henry@gmail.com");
//case 1:Done (Worked well)
// List<string> Listcounter = new List<string>();
// for(int i = 0; i < dt.Rows.Count; i++)
// {
// Listcounter.Add(dt.Rows[i]["counter"].ToString());
// }
// //Result
// var result = string.Join("; ", Listcounter.ToArray());
// MessageBox.Show("e.g: " + result);
///////
//Case:2
/////List
List<model> listModel = new List<model>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string counter = dt.Rows[i]["counter"].ToString();
string name = dt.Rows[i]["name"].ToString();
string email = dt.Rows[i]["e-mail"].ToString();
listModel.Add(new model(counter,name,email));
}
//Result
var result = string.Join("; ", listModel[0].get_Name.ToString() + listModel[0].get_Email.ToString());
MessageBox.Show("e.g: " + result);
///////
预期结果是:仅在消息框中显示姓名和电子邮件
David-David@gmail.com;本-Ben@gmail.com;亨利-Henry@gmail.com
答案 0 :(得分:1)
在Model
类中引入一个方法,该方法将返回已格式化的字符串。然后,如果您不将其用于其他用途,则可以摆脱它们。
public class Model
{
private readonly string _counter;
private readonly string _name;
private readonly string _email;
public Mode(string counter, string name, string email)
{
_counter = counter;
_name = name;
_email = email;
}
public string FormattedData()
{
return $"{_counter}. {_name} - {_email}";
}
}
您可以使用扩展名.Select
循环一次数据来构建整个字符串。
var models = dt.AsEnumerable()
.Select(row => new Model(
row.Field<string>("counter"),
row.Field<string>("name"),
row.Field<string>("e-mail")))
.Select(model => model.FormattedData());
var result = String.Join(", ", models);
Console.WriteLine(result);
// 1. David - David@gmail.com, 2. Ben - Ben@gmail.com, 3. Henry - Henry@gmail.com
正如@UniqIdentifierAssignedAtBirth正确提到的那样,您不需要将集合转换为数组,只需将可枚举传递给String.Join
,该枚举将只被枚举一次。
如果您覆盖ToString
类中的Model
方法,您的代码会变得更加简单,因为String.Join
的泛型重载可以接受任何类型的集合并在每个类型上调用.ToString()
项目。
public class Model
{
private readonly string _counter;
private readonly string _name;
private readonly string _email;
public Mode(string counter, string name, string email)
{
_counter = counter;
_name = name;
_email = email;
}
public override string ToString()
{
return $"{_counter}. {_name} - {_email}";
}
}
// Usage
var models = dt.AsEnumerable()
.Select(row => new Model(
row.Field<string>("counter"),
row.Field<string>("name"),
row.Field<string>("e-mail")));
var result = String.Join(", ", models);
Console.WriteLine(result);
// 1. David - David@gmail.com, 2. Ben - Ben@gmail.com, 3. Henry - Henry@gmail.com
答案 1 :(得分:1)
using System;
using System.Collections.Generic;
using System.Linq;
public class Model
{
private string mCounter;
private string mName;
private string mEmail;
public Model(string mCounter,string mName,string mEmail)
{
this.mCounter = mCounter;
this.mName = mName;
this.mEmail = mEmail;
}
public string MCounter
{
get { return mCounter; }
}
public string MName
{
get { return mName; }
}
public string MEmail
{
get { return mEmail; }
}
# This method will output the results you need for each Model object.
public override string ToString()
{
return MName + " <" + MEmail + ">";
}
}
public class Program
{
public static void Main()
{
List<Model> listModel = new List<Model>();
listModel.Add(new Model("Counter 1", "Name 1", "email1@example.com"));
listModel.Add(new Model("Counter 2", "Name 2", "email2@example.com"));
string combined = string.Join( "; ", listModel.Select(c=>c.ToString()).ToArray<string>());
Console.WriteLine(combined);
}
}
仅供参考,C#代码约定:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions