public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Test t1 = new Test { ID = 23, Age = 23, Name = "测试" };
rtbx.AppendText(t1+"/r/n");
//直接输入tl类名。默认调用了tl的ToString()方法。。。奇怪。。
List<Test> list = new List<Test>
{
new Test{ID=1,Age=10,Name="One"},
new Test(){ID=2,Age=20,Name="Two"},
new Test(){ID=3,Age=30,Name="Three"},
new Test(){ID=4,Age=40,Name="Four"}
};
//var items = from t in list
// where t.Age > 15
// orderby t.ID descending
// select new{编号=t.ID,姓名=t.Name,年龄=t.Age};
var items = list.Where(n => n.Age > 15).OrderByDescending(x => x.ID).Select(y => new { 编号 = y.ID, 姓名 = y.Name, 年龄 = y.Age });
foreach (var i in items)
{
rtbx.AppendText(i+"/r/n");
}
}
}
public class Test
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return "ID:" + ID + " Name:" + Name + " Age:" + Age;
}
}
//在查询语法中需要select子句,但在以前并没有看到Select()方法,因为在LINQ方法语法中不需要它,除非在进行投射(改变结果集中所查询的原始类型)。