在Linq中获取没有空属性的对象

时间:2010-07-27 18:20:57

标签: linq

我想要pa但是空MG道具。

Parent pa = new Parent()
               {
                   MC = new Child[]
                            {
                                new Child() {M1 = 1},
                                new Child() {M1 = 2},
                                new Child() {M1 = 3},
                            },
                   MG = new GrandChild[]
                            {
                                new GrandChild() {M2 = "1"},
                                new GrandChild() {M2 = ""},
                                new GrandChild() {M2 = ""},
                                new GrandChild() {M2 = "4"},
                            },
                   MP = "just string prop",
               };

我知道这不是真的,但想要这样对待我的对象:

var vv = pa.MG.Where(_p=>_p.M2!="").Select(_k=>_k.**parent**) 

1 个答案:

答案 0 :(得分:0)

pa.MG = pa.MG.Where(gc => gc.M2 != "").ToArray();