在ASP.NET MVC 4项目中,我有一个连接模型(with payload):
public class LeagueMember
{
[Key, Column(Order = 0)]
public int MemberId { get; set; }
[Key, Column(Order = 1)]
public int LeagueId { get; set; }
public bool? IsActive { get; set; }
[Required]
public virtual League League { get; set; }
[Required]
public virtual Member Member { get; set; }
}
我正在努力吸引联盟中所有活跃的成员。因此,在联赛模型中,我创建了一个像这样的属性:
public virtual ICollection<LeagueMember> LeagueMembers { get; set; }
public IEnumerable<Member> GetActiveMembers
{
get
{
return LeagueMembers.Select(a => a.IsActive == true ? a.Member : null);
}
}
但是看起来它返回的集合的大小等于所有
Members
的集合(不事件成员的值为空)。是否有更好的方法在匿名方法中应用过滤器以避免空值?
最佳答案
只需在“选择方法”中删除三元条件即可。
public IEnumerable<Member> GetActiveMembers
{
get
{
return from activeMember in LeagueMembers
where activeMember.IsActive == true
select activeMember.Member;
//return LeagueMembers.Select(a => a.IsActive == true);
}
}
关于c# - 使用IEnumerable.Select筛选记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16227965/