[英]Unable to create non-generic LINQ function within Repository class
I'm trying to create a Repository for an Object Page
. 我正在尝试为对象Page
创建存储库。
I would like to have non-generic LINQ queries but can't seem to get them working, 我想使用非通用的LINQ查询,但似乎无法使它们正常工作,
I am only able to create them if they are generic and in a helper class, and please let me know if it is recommended to have this type of function in a Helper Class, let me know the reasoning if so. 如果它们是通用类并且在帮助器类中,则只能创建它们,如果让我们知道是否建议在帮助器类中使用这种类型的功能,请告诉我。
public class PageRepository
{
public PageViewModel GetPageById(string oid)
{
using (var db = new AppDbContext())
{
Page page = db.Pages
//This is what I cannot get working
.WhereStatusType(StatusType.Published);
return GetPageView(page);
}
}
//I would like the below to work
//Tried generic (below), also non-generic <Page> but doesn't work
internal static IQueryable<T> WhereStatusType<T>(this IQueryable<T> query, StatusType statusType = StatusType.Published)
where T : Page
{
return query.Where(p => p.StatusType == statusType);
}
}
//Below is working, but don't want it in a helper
public static class PageHelper
{
internal static IQueryable<T> WhereStatusType<T>(this IQueryable<T> query, StatusType statusType = StatusType.Published)
where T : Page
{
return query.Where(p => p.StatusType == statusType);
}
}
When I attempt to make the IQueryable non-generic, I receive the following errors: 当我尝试使IQueryable为非泛型时,我收到以下错误:
public class PageRepository has an issue: 公共类PageRepository有一个问题:
Extension method must be defined in a non-generic static class 扩展方法必须在非通用静态类中定义
Also: 也:
'IQueryable' does not contain a definition for 'WhereStatusType' and no extension method 'WhereStatusType' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?) “ IQueryable”不包含“ WhereStatusType”的定义,并且找不到扩展方法“ WhereStatusType”,该扩展方法接受“ IQueryable”类型的第一个参数(您是否缺少using指令或程序集引用?)
I'm sure it is a simple solution, thank you for the help. 我确定这是一个简单的解决方案,谢谢您的帮助。 Also, if I am approaching this incorrectly, any advice would be greatly appreciated. 另外,如果我处理不正确,将不胜感激。
While FirstOfDefault() is suggested for the above code, I am also looking to use the function when returning List<PageViewModel>
(I may have over-simplified the code/example), for example: 虽然上面的代码建议使用FirstOfDefault(),但我也希望在返回List<PageViewModel>
时使用该函数(例如,我可能过度简化了代码/示例),例如:
public List<PageViewModel> GetPages()
{
using (var context = new AppDbContext())
{
List<Page> pages = new List<Page>();
pages = context.Pages.AsNoTracking()
.WhereStatusType(StatusType.Published)
.ToList();
if (pages != null)
{
List<PageViewModel> pageViewModels = new List<PageViewModel>();
foreach (Page p in pages)
pageViewModels.Add(GetPageView(p));
return pageViewModels;
}
else
return null;
}
}
The error message tells you exactly what's going on here. 该错误消息告诉您确切的情况。 If you want an extension method -- a method whose first parameter uses the this
keyword, like your example -- then it needs to be defined in a non-generic static class, like your PageHelper
. 如果需要扩展方法(例如第一个参数使用this
关键字的方法,例如您的示例),则需要在非泛型静态类(如PageHelper
。
If you don't like that approach, you could define a non-extension method, something like this: 如果您不喜欢这种方法,则可以定义一个非扩展方法,如下所示:
public class PageRepository
{
public PageViewModel GetPageById(string oid)
{
using (var db = new AppDbContext())
{
Page page = GetPagesWithStatus(db.Pages, StatusType.Published);
return GetPageView(page);
}
}
internal IQueryable<Page> GetPagesWithStatus(IQueryable<Page> query, StatusType statusType)
{
return query.Where(p => p.StatusType == statusType);
}
}
As @asherber mentioned, your LINQ query isn't right. 如@asherber所述,您的LINQ查询不正确。 You are comparing p.Alias
to p.Alias
您正在比较p.Alias
与p.Alias
Also, it looks like you want a FirstOrDefault()
in there. 另外,您似乎想要在其中FirstOrDefault()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.