我有一個Address類。如何在某些列中使用linq&linq中的Distinct到NHibernate
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中的:省&市
即類似於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
Quivalent查詢:select distinct Province , City from tblAddress
我也測試了此查詢的LINQ to 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.
我該怎麼辦?
在LINQ to對象匿名類型是正確的,但在LINQ to NH無效。因爲它我必須做什麼? – Ehsan
@Ehsan:嘗試第一個選項 –
我認爲你可以在第一個查詢中使用Key而不是First。 – Euphoric