Question
有没有办法在 C# 中(在辅助类或其他类中)只定义一次方法,而不知道要返回哪种类型?
长解释我收到以下错误:
无法转换类型的对象
系统.数据.对象.ObjectQuery1[WerkStageNu.Vacancies]'
to type
'System.Linq.IQueryable
1[WerkStageNu.Models.IFilteredEntities]'。
我有一个 ListingsController,它可以搜索数据库中当前的职位空缺:
public ActionResult Search(int? page, string branchid, string hoursago, string jobtypeid, string educationlevelid, string careerlevelid)
{
string searchResult = string.Empty;
const int pageSize = 10;
IQueryable<IFilteredEntities> selectedListings = (IQueryable<IFilteredEntities>)Repository.Instance._entities.Vacancies.AsQueryable();
Dictionary<string, string> filterParams = new Dictionary<string, string>() {
{"branchid", branchid}, {"hoursago", hoursago}, {"jobtypeid", jobtypeid}, {"educationlevelid", educationlevelid}, {"careerlevelid", careerlevelid}};
selectedListings = FilterByIDHelper.Filter(selectedListings, filterParams);
var paginatedDinners = new PaginatedList<Vacancies>(((IQueryable<Vacancies>)selectedListings).ToList(), page ?? 0, pageSize);
return View("Index", paginatedDinners);
}
现在,此搜索仅针对职位空缺。但可以想象我们在各处进行搜索,通常都是相同的例程,所以我想调用相同的方法来返回不同的类型。对于这种情况,我创建了一个 Interface , IFilteredEntities。在我的部分类空缺(部分类,类空缺是由我的数据库实体框架生成)中,我只是这样做:
public partial class Vacancies : IFilteredEntities
当然还要实现接口中默认未实现的方法。在我的界面中我有:
interface IFilteredEntities
{
string EducationLevelID { get; set; }
string BrancheID { get; set; }
string CareerLevelID { get; set; }
string JobTypeID { get; set; }
Branches Branches { get; set; }
DateTime? DateOfCreation { get; set; }
CareerLevels CareerLevels { get; set; }
JobTypes JobTypes { get; set; }
EducationLevels EducationLevels { get; set; }
}
为了方便起见,我上传了两个帮助器类PaginatedList和FilterCriteria Helperhere http://www.bastijn.nl/zooi/PaginatedList.cs and here http://www.bastijn.nl/zooi/FilterCriteriaHelper.cs.
现在,执行实际过滤的方法被放置在另一个帮助器类中:FilterByIDHelper.cs。
public static IQueryable<IFilteredEntities> Filter(IQueryable<IFilteredEntities> collection, Dictionary<string, string> filterParams)
{
if (filterParams.ContainsKey("branchid")) collection = FilterByBranchId(collection, filterParams["branchid"]);
if (filterParams.ContainsKey("hoursago")) collection = FilterByHoursAgo(collection, filterParams["hoursago"]);
if (filterParams.ContainsKey("jobtypeid")) collection = FilterByJobTypeId(collection, filterParams["jobtypeid"]);
if (filterParams.ContainsKey("educationlevelid")) collection = FilterByEducationLevelId(collection, filterParams["educationlevelid"]);
if (filterParams.ContainsKey("careerlevelid")) collection = FilterByCareerLevelId(collection, filterParams["careerlevelid"]);
return collection;
}
public static IQueryable<IFilteredEntities> Filter(IQueryable<IFilteredEntities> collection, Dictionary<string, string> filterParams)
{
if (filterParams.ContainsKey("branchid")) collection = FilterByBranchId(collection, filterParams["branchid"]);
if (filterParams.ContainsKey("hoursago")) collection = FilterByHoursAgo(collection, filterParams["hoursago"]);
if (filterParams.ContainsKey("jobtypeid")) collection = FilterByJobTypeId(collection, filterParams["jobtypeid"]);
if (filterParams.ContainsKey("educationlevelid")) collection = FilterByEducationLevelId(collection, filterParams["educationlevelid"]);
if (filterParams.ContainsKey("careerlevelid")) collection = FilterByCareerLevelId(collection, filterParams["careerlevelid"]);
return collection;
}
为了方便起见,这里是我的解决方案资源管理器的一部分的图片:
解决方案资源管理器 http://www.bastijn.nl/zooi/solutionexplorer.png http://www.bastijn.nl/zooi/solutionexplorer.png
简而言之:
我尝试做的不是像这样调用:
selectedListings = Repository.Instance._entities.Vacancies.AsQueryable();
Dictionary<string, string> filterParams = new Dictionary<string, string>() {
{"branchid", branchid}, {"hoursago", hoursago}, {"jobtypeid", jobtypeid}, {"educationlevelid", educationlevelid}, {"careerlevelid", careerlevelid}};
selectedListings = FilterByIDHelper.Filter(selectedListings, filterParams);
var paginatedDinners = new PaginatedList<Vacancies>(selectedListings.ToList(), page ?? 0, pageSize);
return View("Index", paginatedDinners);
使用接口调用显示的变体,因此我只需定义一次“Filter”方法,而不是为所有类/模型定义一次。现在请注意,所有这些都可以编译!问题是我收到以下错误:
Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[WerkStageNu.Vacancies]' to type 'System.Linq.IQueryable`1[WerkStageNu.Models.IFilteredEntities]'.
我希望我没有忘记任何信息,但我已经盯着这段代码一段时间了。可能会忘记某个关系或其他什么,如果我忘记了就问一下:)。
-------------------------------------------------- ---
评论后编辑
-------------------------------------------------- ---
噢,废话,别介意这部分,我忘了作为 AsEnumerable,仍在使用 AsQueryable。