在EF GetById是T类型但我需要一个List?

时间:2013-12-29 18:43:49

标签: c# asp.net-mvc asp.net-mvc-3 entity-framework entity-framework-4

IGroupTypeRepository groupTypeRepo = new GroupTypeRepository();

IGroupRepository groupRepo = new GroupRepository();

var model = new GroupModels();
model.GroupTypeNames = groupTypeRepo.GetAll().ToList();
//now I want to show all group info group only 1st group type so used like following
Guid first = model.GroupTypeNames.FirstOrDefault().Id;
//in below code i can't simply do like groupRepo.GetById(first).ToList() as GetById is of T
//but its important to get all group values into model.GroupNames             
model.GroupNames = groupRepo.GetById(first);

return View("Groups", model);

如何将所有组值都放入{1}组中的1号组类型。

3 个答案:

答案 0 :(得分:1)

您可以使用Where子句来执行此操作,而不是使用GetById,如下所示。它将返回匹配的列表

model.GroupNames = groupRepo.GetAll().Where(s=>s.GroupTypeID==first).ToList();

答案 1 :(得分:0)

这样的事情应该有效(将SomeProperty改为你的实际属性名称):

model.GroupNames = groupRepo.GetAllGroups().Where(x=>x.SomeProperty==first).ToList();

答案 2 :(得分:0)

你应该这样做。

model.GroupNames=groupRepo.GetAllGroups(s=>s.GroupTypeID==first).toList();

假设您的存储库中的GetAllGroups方法返回Group的集合,并且每个组都有一个名为GroupTypeID的属性,您希望将其用于过滤。此外,上面的代码假定您的模型的GroupNames属性是类型,Group的集合。