Can I generate a linq expression dynamically in C#?

后端 未结 4 993
遥遥无期
遥遥无期 2021-01-22 08:40

I\'ve currently got a piece of Linq that looks something like this ;

List childrenToBeRemoved = this.ItemsSource.Where(o => o.ParentID == \"123         


        
4条回答
  •  余生分开走
    2021-01-22 09:30

    If you know the type of your items, you can use reflection :

    PropertyInfo parentProp = itemType.GetProperty("ParentKey or whatever");
    List childrenToBeRemoved = this.ItemsSource.Where(o => Equals("1234", parentProp.GetValue(o, null))).ToList();
    

提交回复
热议问题