IEnumerable过滤器

时间:2014-07-21 21:31:40

标签: c# linq ienumerable

如何添加过滤器以便我只返回RelationshipTypeIDs 26-30?

public IEnumerable<SelectListOptions> GetRelationshipTypes()
{
    return (from q in unitOfWork.GenericRepository<tlkpRelationshipType>().Get()
            select new SelectListOptions
            {
                Value = q.RelationshipTypeID.ToString(),
                Label = q.RelationshipType
            }
            ).ToList();
}

2 个答案:

答案 0 :(得分:3)

使用where子句,如下所示:

public IEnumerable<SelectListOptions> GetRelationshipTypes()
{
    return (from q in unitOfWork.GenericRepository<tlkpRelationshipType>().Get()
            where q.RelationshipTypeID >= 26 && q.RelationshipTypeID <= 30
            select new SelectListOptions
            {
                Value = q.RelationshipTypeID.ToString(),
                Label = q.RelationshipType
            }
            ).ToList();
}

答案 1 :(得分:1)

return (from q in unitOfWork.GenericRepository<tlkpRelationshipType>().Get()
        where q.RelationshipTypeID >= 26
        where q.RelationshipTypeID <= 30
        select new SelectListOptions