Linq 쿼리에 Expression을 전달하는 것은 사용 된 구문에 따라 다르게 동작하며, 왜 그런지 궁금합니다.Linq 여기서 keyword 대 where 확장 및 표현 매개 변수
의 난이 아주 일반적인 기능을 가지고 있다고 가정 해 봅시다
private IEnumerable<Company>
GetCompanies(Expression<Func<Company, bool>> whereClause)
{
return (from c in _ctx.Companies.Where(whereClause) select c);
}
을 예상하지만 다음의 구현은 고려하지 않습니다 (위임 'System.Func'를 컴파일하지 않는 한 다음 구현이 작동
private IEnumerable<Company>
GetCompanies(Expression<Func<Company, bool>> whereClause)
1 개의 인수)
private IEnumerable<Company>
GetCompanies(Expression<Func<Company, bool>> whereClause)
{
return (from c in _ctx.Companies where whereClause select c);
}
분명히 첫 번째 구문을 사용할 수는 있지만 컴파일러가 where 키워드를 Where 확장명과 동일하게 취급하지 않는 이유는 무엇입니까?
덕분에, 토마스
Expression> 대신 Func 을 인수로 사용하려면 매개 변수 유형으로 술부 을 사용할 수있었습니다. –
네, 이것은 '어디'의 차이에만 초점을 맞추기위한 단순화 된 예입니다. Expression>을 사용하는 이유는 메모리에서가 아니라 데이터베이스에서 어디에서 실행해야하는지입니다. –