나는 내 머리 속에서이 머리글을 긁어 왔지만 여전히 그 원인을 파악하지 못했습니다.리포지토리 패턴을 사용하는 SQL에 Linq : 개체가 SQL에 대한 번역을 지원하지 않습니다
두 개의 다른 리포지토리를 참조하는 복합 리포지토리 개체가 있습니다. LINQ 쿼리에서 Model 유형을 인스턴스화하려고합니다 (첫 번째 코드 스 니펫 참조).
public class SqlCommunityRepository : ICommunityRepository {
private WebDataContext _ctx;
private IMarketRepository _marketRepository;
private IStateRepository _stateRepository;
public SqlCommunityRepository(WebDataContext ctx, IStateRepository stateRepository, IMarketRepository marketRepository)
{
_ctx = ctx;
_stateRepository = stateRepository;
_marketRepository = marketRepository;
}
public IQueryable<Model.Community> Communities
{
get
{
return (from comm in _ctx.Communities
select new Model.Community
{
CommunityId = comm.CommunityId,
CommunityName = comm.CommunityName,
City = comm.City,
PostalCode = comm.PostalCode,
Market = _marketRepository.GetMarket(comm.MarketId),
State = _stateRepository.GetState(comm.State)
}
);
}
}
}
내가 좋아하는 모습을 전달하고있어 리포지토리 개체이
public class SqlStateRepository : IStateRepository {
private WebDataContext _ctx;
public SqlStateRepository(WebDataContext ctx) { _ctx = ctx; }
public IQueryable<Model.State> States
{
get
{
return from state in _ctx.States
select new Model.State()
{
StateId = state.StateId,
StateName = state.StateName
};
}
}
public Model.State GetState(string stateName)
{
var s = (from state in States
where state.StateName.ToLower() == stateName
select state).FirstOrDefault();
return new Model.State()
{
StateId = s.StateId,
StateName = s.StateName
};
}
및
public class SqlMarketRepository : IMarketRepository {
private WebDataContext _ctx;
public SqlMarketRepository(WebDataContext ctx)
{
_ctx = ctx;
}
public IQueryable<Model.Market> Markets
{
get
{
return from market in _ctx.Markets
select new Model.Market()
{
MarketId = market.MarketId,
MarketName = market.MarketName,
StateId = market.StateId
};
}
}
public Model.Market GetMarket(int marketId)
{
return (from market in Markets
where market.MarketId == marketId
select market).FirstOrDefault();
}
}
내가 모든 걸 배선있어 방법이있다 :
WebDataContext ctx = new WebDataContext();
IMarketRepository mr = new SqlMarketRepository(ctx);
IStateRepository sr = new SqlStateRepository(ctx);
ICommunityRepository cr = new SqlCommunityRepository(ctx, sr, mr);
int commCount = cr.Communities.Count();
위 스 니펫의 마지막 줄은 실패한 부분입니다. 인스턴스화 (new Model.Community)를 통해 디버깅 할 때 다른 저장소 메소드 중 하나로 들어 가지 않습니다. 이 세 가지 객체 뒤에있는 기본 테이블 간에는 관계가 없습니다. 이것은 LINQ to SQL이 표현식 트리를 올바르게 구축 할 수없는 이유입니까? 다음과 같습니다 다른 저장소 방법을 추가
그랬습니다. IQueryable에서 메서드를 호출 할 때까지 쿼리가 실행되지 않습니다? – Praveen
대부분의 Linq 메서드는 지연되어 결과가 열거 될 때까지 쿼리를 평가하지 않습니다. foreach와 ToList는 둘 다 쿼리를 열거하는 수단입니다. –