예, 초록에 관한 질문을하는 것이 4 일째되는 날입니다. 죄송합니다. SQLServer에 대한 몇 가지 질문에 답변을 드리겠습니다. 어쨌든 ...Linq 결과를 추상 생성자로 어떻게 투영합니까?
Linq 쿼리의 결과를 추상 기본 클래스 컬렉션에 어떻게 투영 할 수 있습니까? 여기 내 RecruiterBase 추상 클래스에서 내 내 방법 (해당 CandidateBase 추상 클래스도 있습니다)입니다 :
public IQueryable<CandidateBase> GetCandidates()
{
return from candidates in db.Candidates
where candidates.RecruiterId == this.RecruiterId
select candidates;
}
위의 방법은 암시 적 변환이 후보와 CandidateBase 사이 수 없다는 컴파일시 오류가 발생합니다.
db.Candidates.Cast()에 db.Candidates를 수정하면 모든 사항을 컴파일 할 수 있지만 런타임 오류가 발생합니다. 즉, 유형이 Candidate와 CandidateBase 사이에 정의되지 않습니다.
할 수 없습니다. 추상을 구현할 수 없으므로 CandidateBase로 새 CandidateBase {...}를 선택하십시오. 다시 내 추상적까지 새에 저를 필요로하기 때문에
은 아니다 내가 후보와 후보베이스 사이의 명시 적 변환 연산자를 만들 수 있습니다
도 아니다 나는 익명의 객체로 내 결과를 투영 내가 먹을수록 다음 CandidateBase으로 캐스팅 할 수 있습니다 익명의 형태와 CandidateBase의 형태와 같은 실행시 강제 변환 예외.
이 문제는 스탠 R에서 제공하는 대답은 내가 복잡한 물건을 만드는 것을이었다 Problem with Covariant return types from an abstract method
, 어제의 질문에서 약왔다. 난 다시 가서 간단하게 모든 것을 (I베이스에 implmentation을 떠나 잠수정에서 제거) 및 작업 GetCanidates의 같은 구현 방법 결국 :
이public IQueryable<CandidateBase> GetCandidates()
{
return (from candidates in db.Candidates
where candidates.RecruiterId == this.RecruiterId
select new CandidateA
{
CandidateId = candidates.CandidateId,
LastName = candidates.LastName,
RecruiterId = candidates.RecruiterId
}).Cast<CandidateBase>();
}
위의 방법은 컴파일 작품, 나는 ' 입안에서 선물용 말을 보려고하지 않고, 이제는 기본 유형의 하위 유형에 대한 참조 (CandidateA에 결과를 투영 할 때)가 있으며 이상하게 보입니다. 기본 유형 내에서 하위 유형에 대한 참조가 괜찮 으면 내 질문에 자유롭게 투표하십시오.
감사합니다.
전체 클래스 인증 된 정의 :
public abstract class RecruiterBase
{
public int RecruiterId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public RecruiterBase()
{
}
public RecruiterBase(int id)
{
DataClasses1DataContext db = new DataClasses1DataContext();
Recruiter rc = db.Recruiters.SingleOrDefault(r => r.RecruiterId == id);
this.RecruiterId = rc.RecruiterId;
this.FirstName = rc.FirstName;
this.LastName = rc.LastName;
}
public IQueryable<CandidateBase> GetCandidates()
{
DataClasses1DataContext db = new DataClasses1DataContext();
return (from candidates in db.Candidates
where candidates.RecruiterId == this.RecruiterId
select new CandidateA
{
CandidateId = candidates.CandidateId,
LastName = candidates.LastName,
FirstName = candidates.FirstName,
RecruiterId = candidates.RecruiterId
}
).Cast<CandidateBase>();
}
}
public abstract class TempCandidateBase
{
public int CandidateId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? RecruiterId { get; set; }
public CandidateBase()
{
}
public CandidateBase(int id)
{
DataClasses1DataContext db = new DataClasses1DataContext();
Candidate candidate = db.Candidates.SingleOrDefault(c => c.CandidateId == id);
this.CandidateId = candidate.CandidateId;
this.FirstName = candidate.FirstName;
this.LastName = candidate.LastName;
this.RecruiterId = candidate.RecruiterId;
}
}
public class RecruiterA : RecruiterBase
{
public RecruiterA()
: base()
{
}
public RecruiterA(int id)
: base(id)
{
}
}
public class CandidateA : CandidateBase
{
public CandidateA()
: base()
{
}
public CandidateA(int id)
: base(id)
{
}
}
당신이 우리에게 후보자 및 CandidateBase 클래스뿐만 아니라 실제 관련 컴파일러 에러 메시지를 표시 할 수? –
정확한 게시판을 사용하여 잠시 편집 한 게시물을 보내 드리겠습니다. –