16,555
社区成员
发帖
与我相关
我的任务
分享
https://www.cnblogs.com/itheo/p/14358495.html#3%E6%9E%84%E5%BB%BA%E8%A1%A8%E8%BE%BE%E5%BC%8F
《通过创建动态类型 动态构建Expression Select表达式来控制Property可见性》这个文章略为修改完美解决了我的问题。
Public Class student
Public name As String
Public sex As String
Public age As Integer
End Class
Public 二年一班 As New List(Of student)
Public Function 学生选择器(表达式 As Func(Of student, student)) As IEnumerable(Of student)
Return 二年一班.Select(表达式)
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim p1 = New student With {.name = "张三", .age = 8, .sex = "男"}
Dim p2 = New student With {.name = "李四", .age = 9, .sex = "女"}
Dim p3 = New student With {.name = "王五", .age = 8, .sex = "男"}
Dim p4 = New student With {.name = "赵六", .age = 7, .sex = "女"}
二年一班.Add(p1)
二年一班.Add(p2)
二年一班.Add(p3)
二年一班.Add(p4)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selected = 学生选择器(Function(s)
Return New student With {.name = s.name, .sex = s.sex}
End Function).ToList
End Sub
public static void Main(string[] args)
{
var q = Test(x => new User{ID=x.ID,Name = x.Name });
Console.ReadLine();
}
public static IEnumerable<User> Test(Func<User, User> selector)
{
return myList.Select(selector);
}
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool? IsChild { get; set; }
}