나는 Address 클래스가 있습니다.사용 방법 일부 linq 및 linq에서 NHibernate와 구별
나는 두 개의 열하여이 myList에의 독특한 추출 할 필요가var myList = new List<Address>
{
new Address {Province = "P1", City = "C1", PostalCode = "A"},
new Address {Province = "P1", City = "C1", PostalCode = "B"},
new Address {Province = "P1", City = "C1", PostalCode = "C"},
new Address {Province = "P1", City = "C2", PostalCode = "D"},
new Address {Province = "P1", City = "C2", PostalCode = "E"},
new Address {Province = "P2", City = "C3", PostalCode = "F"},
new Address {Province = "P2", City = "C3", PostalCode = "G"},
new Address {Province = "P2", City = "C3", PostalCode = "H"},
new Address {Province = "P2", City = "C4", PostalCode = "I"}
};
: 지방 & 도시
즉 유사한 myExpertResult
에 :
var myExpertResult = new List<Address>
{
new Address {Province = "P1", City = "C1"},
new Address {Province = "P1", City = "C2"},
new Address {Province = "P2", City = "C3"},
new Address {Province = "P2", City = "C4"}
};
이 기본 값으로
public class Address : RootEntityBase
{
virtual public string Province { set; get; }
virtual public string City { set; get; }
virtual public string PostalCode { set; get; }
}
그래서이 코드를 사용합니다 :
var list = myList.Select(x => new Address {City = x.City, Province = x.Province}).Distinct().ToList();
결과가 유효하지 않으므로 결과가 유효하지 않습니다. 즉 모든 주소가 9입니다. SQL에서
Quivalent 쿼리는 다음과 같습니다
select distinct Province , City from tblAddress
도 내가 NHibernate에에 LINQ하여이 쿼리를 테스트했다.
var q = SessionInstance.Query<Address>();
.Select(x => new Address { Province = x.Province, City = x.City }).Distinct().ToList();
그러나이 쿼리는 지원되지 않습니다. 예외 메시지 : Expression type 'NhDistinctExpression' is not supported by this SelectClauseVisitor.
어떻게하면됩니까?
var result = myList.GroupBy(a => new { a.Province, a.City })
.Select(g => new Address {
Province = g.Key.Province,
City = g.Key.City
});
을 또는 익명 형식을 사용합니다 :
익명 형식이 올바르지 만 NH에 LINQ에 유효하지 않습니다. 그것을 위해 내가 뭘해야합니까? – Ehsan
@Ehsan : 첫 번째 옵션을 시도하십시오 –
첫 번째 쿼리에서 첫 번째 키 대신 키를 사용할 수 있다고 생각합니다. – Euphoric