Type member support in LINQ-to-Entities?에서 LINQ에서 쿼리 할 클래스 속성을 선언하려고 시도했지만 문제가 발생했습니다. 여기서는 구현 내부에서 코드를 쿼리로 변환하기위한 도움을 얻기 위해 코드를 배치합니다.루프를 단일 linq 표현식으로 병합하기
는 I가 QuestionLevel
에 따라 분류되고, 각각의 Question
(S)의 컬렉션을 포함하는 클래스 Quiz
가 ... I 퀴즈가 통해 수행되는 "개방"또는 "폐쇄"인지 여부를 결정해야 최대 값 표와 비교하여 질문 수준 및 각 수준의 질문 수에 대한 외부 조인 그래서 여기
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; }
조인 절에있는 식 중 하나가 잘못되었습니다. '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;
}
하지만 런타임 예외를 생산, 나는 그것이 된 IQueryable에 QuestionLevels을 캐스팅 날 필요로 ... 컴파일 할 수있는 표현을 얻을 수 없다 (하지만 캐스팅이 작동하지 않습니다). *
* 업데이트 II는 내가 캐스팅 문제에 대한 해결책을 찾았지만 지금은 다시 "다른 컨텍스트"예외입니다. grr ...
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를 지원하며 사용자 코드에서 직접 사용할 수 없습니다 된다.
그래서 정확히 무엇이 문제이며, 무엇을하려고합니까? 현재 코드가 작동합니까? 이 질문을 정돈하여 정확하게 달성하려는 내용과 문제가 무엇인지 명확히 설명 할 수 있습니다. –
@KirkBroadhurst, 내가 가진 문제는 내 확장의 중간에 조회를해야하며 두 가지 상황에 합류 할 수없는 것입니다. – ekkis
@KirkBroadhurst, 조금이라도 반가워하면 죄송합니다. 그것은 내가 타격을 가하는 많은 장애물을 보여주기위한 것입니다. 그리고 아니오,이 시점에서 코드가 작동하지 않습니다. QuestionLevels에 가입하면 두 개의 컨텍스트가 있다고 생각하게됩니다.하지만 QuestionLevel에 메모리 내 개체가 있어야하기 때문에 실제로는 없어야합니다 ... 이해가 안됩니다. – ekkis