Linq:检查列表中的某个属性是否为所有对象的null

时间:2017-12-05 15:16:42

标签: c# linq

public class P 
{
 public string Name {get; set;}
 public string Value {get; set;}
 public Student Student {get; set;}
}

List<P> p = new List<P>();
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})        
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "1", Value = null, Student = new Student{Name = "1"}})
p.Add(new P{Name = "2", Value = null, Student = new Student{Name = "2"}})
p.Add(new P{Name = "1", Value = "xxx", Student = new Student{Name = "2"}})

我想按 P.Name 进行分组,只获取具有P.Value所有缺失值的元素。

在这个例子中我想得到“1”

如果我需要分组 P.Student.Name

1 个答案:

答案 0 :(得分:5)

您可以将require(dplyr) # manipulate the data pdf <- df %>% mutate(id = row_number()) %>% gather(key, value, -id) %>% group_by(key) %>% mutate(min = min(value), max = max(value), mean = if_else(key != 'x1', mean(value), value), norm = (mean-min)/(max-min)) # Draw plot ggplot(pdf, aes(x = key, y = norm, group = id)) + geom_path(aes(color = id), alpha = 0.5, lineend = 'round', linejoin = 'round') GroupBy一起用于每个群组:

All

修改 如果您需要按var r = p.GroupBy(x => x.Name) .Where(g => g.All(x => x.Value == null)) .Select(g=> g.Key); 进行分组,则只需在Student.Name中指定该属性即可。您可以根据要用于分组的项目指定任何表达式。

GroupBy