一、string.Join 合并字符串
struct Student
{
public string No;
public string Name;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//示例一:
List<string> arg = new List<string>();
arg.Add("a");
arg.Add("b");
string s1 = string.Join("/", arg);
MessageBox.Show(s1);
//示例二:
List<Student> studentList = new List<Student>();
studentList.Add(new Student() { No = "A01", Name = "李平" });
studentList.Add(new Student() { No = "A02", Name = "陈明" });
string s2 = string.Join("/", studentList.OrderBy(o => o.No).Select(o => o.Name));
MessageBox.Show(s2);
}
二、List<T>.ForEach 方法,对 List<T> 的每个元素执行指定操作。
struct Student
{
public string No;
public string Name;
public string Sex;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
List<Student> studentList = new List<Student>();
studentList.Add(new Student() { No = "A1", Name = "秦天明", Sex = "男" });
studentList.Add(new Student() { No = "A2", Name = "陈一星", Sex = "男" });
studentList.Add(new Student() { No = "B1", Name = "李明珠", Sex = "女" });
studentList.Add(new Student() { No = "B2", Name = "叶小娴", Sex = "女" });
//将男生放入maleList中
List<Student> maleList = new List<Student>();
studentList.Where(o => o.Sex == "男").ToList().ForEach(o => maleList.Add(o));
MessageBox.Show(string.Join("/", maleList.Select(o=>o.Name)));
//将女生放入feMaleList中
List<Student> feMaleList = new List<Student>();
studentList.Where(o => o.Sex == "女").ToList().ForEach(o => feMaleList.Add(o));
MessageBox.Show(string.Join("/", feMaleList.Select(o=>o.Name)));
}
三、合计 var myDm = ((PubReceiveDm)dm);
var totalPay = myDm.DetailView.Cast<DataRowView>().Sum(o=>o.Row.Field<decimal>("PayAmt"));
dm.CurrentItem["Amount"] = totalPay;