I have an Address class.
public class Address : RootEntityBase { virtual public string Province { set; get; } virtual public string City { set; get; } virtual public string PostalCode { set; get; } }
Default:
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"} };
I need to extract a selection other than this myList with two columns: province and city
Namely, similar to 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"} };
So, I am using this code:
var list = myList.Select(x => new Address {City = x.City, Province = x.Province}).Distinct().ToList();
but my result is invalid, since the calculation of the result is 9, namely to all addresses.
SQL qualification query: select distinct Province , City from tblAddress
also i checked this linq request on NHibernate.
var q = SessionInstance.Query<Address>(); .Select(x => new Address { Province = x.Province, City = x.City }).Distinct().ToList();
But he does not support this request. Exception Message: Expression type 'NhDistinctExpression' is not supported by this SelectClauseVisitor.
How can i do this?