In LINQ-to-Entities 中的类型成员支持?我试图声明一个要在 LINQ 中查询的类属性,但遇到了一些问题。在这里,我将列出实现中的代码,希望对将其转换为查询有所帮助。
我有课Quiz
其中包含一个集合Question
s,其中每一个都根据QuestionLevel
...我需要确定测验是“开放”还是“封闭”,这是通过问题级别的外连接以及每个级别中的问题计数(与最大值表相比)来完成的。这是逐字的代码:
public partial class Quiz
{
public bool IsClosed
{
get
{
// if quiz has no questions, it's open
if (this.Questions.Count() == 0) return false;
// get a new handle to the EF container to do a query for max values
using (EFContainer db = new EFContainer())
{
// we get a dictionary of LevelName/number
Dictionary<string, int> max = db.Registry
.Where(x => x.Domain == "Quiz")
.ToDictionary(x => x.Key, x => Convert.ToInt32(x.Value));
// count the number of questions in each level, comparing to the maxima
// if any of them are less, the quiz is "open"
foreach (QuestionLevel ql in db.QuestionLevels)
{
if (this.Questions.Where(x => x.Level == ql).Count() < max["Q:Max:" + ql.Name])
return false;
}
}
// the quiz is closed
return true;
}
}
}
所以这是我尚未进行的尝试:
public static IQueryable<Quiz> WhereIsOpen(this IQueryable<Quiz> query)
{
EFContainer db = new EFContainer();
return from ql in db.QuestionLevels
join q in query on ql equals q.Questions.Select(x => x.Level)
into qs
from q in qs.DefaultIfEmpty()
where q.Questions.Count() < db.Registry
.Where(x => x.Domain == "Quiz")
.Where(x => x.Key == "Q:Max" + ql.Name)
.Select(x => Convert.ToInt32(x.Value))
select q;
}
它在加入时失败,抱怨:
join 子句中的表达式之一的类型不正确。
调用“GroupJoin”时类型推断失败
我还在想办法解决这个问题。
*更新我*
啊。愚蠢的我。
join q in query on ql equals q.Questions.Select(x => x.Level).Single()
又一个障碍:
指定的 LINQ 表达式包含对以下查询的引用
与不同的上下文相关。
这是因为我为最大查找次数创建了新容器;所以我想像这样重新考虑:
public static IQueryable<Quiz> WhereIsOpen(this IQueryable<Quiz> query)
{
EFContainer db = new EFContainer();
IEnumerable<QuestionLevel> QuestionLevels = db.QuestionLevels.ToList();
Dictionary<string, int> max = db.Registry
.Where(x => x.Domain == "Quiz")
.ToDictionary(x => x.Key, x => Convert.ToInt32(x.Value));
return from ql in QuestionLevels
join q in query on ql equals q.Questions.Select(x => x.Level).Single()
into qs
from q in qs.DefaultIfEmpty()
where q.Questions.Count() < max["Q:Max:" + ql.Name]
select q;
}
但我无法编译表达式...它需要我将 QuestionLevels 转换为 IQueryable (但转换不起作用,会产生运行时异常)。
*更新二*
我找到了铸造问题的解决方案,但现在我又回到了“不同上下文”异常。呃……
return from ql in QuestionLevels.AsQueryable()
*更新(柯克的建议)*
所以我现在有了这个,它可以编译但会生成运行时异常:
public static IQueryable<Quiz> WhereIsOpen(this IQueryable<Quiz> query)
{
EFContainer db = new EFContainer();
IEnumerable<string> QuestionLevels = db.QuestionLevels.Select(x => x.Name).ToList();
Dictionary<string, int> max = db.Registry
.Where(x => x.Domain == "Quiz")
.ToDictionary(x => x.Key, x => Convert.ToInt32(x.Value));
return from ql in QuestionLevels.AsQueryable()
join q in query on ql equals q.Questions.Select(x => x.Level.Name).Single()
into qs
from q in qs.DefaultIfEmpty()
where q.Questions.Count() < max["Q:Max:" + ql]
select q;
}
然后我这样称呼它:
List<Product> p = db.Quizes.WhereIsOpen().Select(x => x.Component.Product).ToList();
结果异常:
此方法支持 LINQ to Entities 基础结构,但不支持
旨在直接从您的代码中使用。