-3

我有一个项目列表,其中某些项目可能具有特定项目属性的空值/零值。我正在按其他属性对项目进行分组。然后我想要的就是选择我的属性!= null/zero 的那些组的 First() 元素,以及该属性等于 null/zero 的组中的所有项目。怎么做?

4

1 回答 1

0

由于您的问题中没有相关的数据和代码,所以让我通过示例给您一个一般性的答案:

首先,让我们有一个示例类:

class Test
{
    public int? First { get; set; }
    public int? Second { get; set; }
    public int? Third { get; set; }
}

...然后让我们填充它:

        List<Test> list = new List<Test> 
        {
            new Test {First = null, Second = 1, Third = 2},
            new Test {First = 5, Second = 3, Third = 4},
            new Test {First = 6, Second = null, Third = 8},
            new Test {First = null, Second = 2, Third = 5},
            new Test {First = 8, Second = null, Third = 14},
            new Test {First = 21, Second = 2, Third = null},
            new Test {First = 5, Second = 4, Third = 2},
            new Test {First = 9, Second = 0, Third = 8}
        };

...现在你可以像这样做一些事情(如果我理解你的问题的话):

        var result = list
            .GroupBy(x => x.First) //grouping by First property
            .Select(y => new
                               {
                                   FirstNonNull = y.FirstOrDefault(z => z.Second != null), // first null in group by Second property
                                   AllNull = y.Where(f => f.Second == null) // all null in group by Second property
                               }
            );
于 2013-05-23T17:56:05.040 回答