你可能想在MoreLinq看看DistinctBy方法。你可以在哪裏傳遞一個lambda表達式作爲參數。
在你的榜樣,將類似於這樣:
var sourceId =
(from workstation in db.station
join letter in db.letter on workstation.id equals letter.Site_Id
where ThatDate < DateTime.Now.AddYears(-1)
select workstatio).DistinctBy(ws => ws.Id).ToList();
您還可以使用DistinctBy具有多個屬性:
var sourceId =
(from workstation in db.station
join letter in db.letter on workstation.id equals letter.Site_Id
where ThatDate < DateTime.Now.AddYears(-1)
select workstatio).DistinctBy(ws => new {Id = ws.Id, OtherProperty = ws.OtherProperty}).ToList();
順便說一句,而不是參考比較,匿名平等類型實例是通過比較其每個屬性來確定的。
我最近在Anders Hejlsberg本人建議解決此問題的方法中發現了一個有趣的討論。你可以閱讀它here.
可能的重複[LINQ不同的類項目?](http://stackoverflow.com/questions/1930654/linq-distinct-on-class-item) –
我假設工作站:信是一對多的嗎? – IronMan84