Addressクラスがあります。
public class Address : RootEntityBase
{
virtual public string Province { set; get; }
virtual public string City { set; get; }
virtual public string PostalCode { set; get; }
}
このデフォルト値では:
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"}
};
このmyListを2つの列で区別する必要があります:Province&City
つまり、に似てい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"}
};
だから私はこのコードを使用します:
var list = myList.Select(x => new Address {City = x.City, Province = x.Province}).Distinct().ToList();
しかし、結果のカウントが9、つまりすべてのアドレスであるため、私の結果は無効です。
SQLの同等のクエリは次のとおりです。select distinct Province , City from tblAddress
また、linqからNHibernateへのこのクエリをテストしました。
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.
どうすればいいですか?