我正在做以下工作:
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
city = r.orderAddress.BilltoCity
}).Distinct().ToList();
查看 city = r.orderAddress.BilltoCity 行,有什么方法可以填充整个对象......就像这样
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
Address = r.orderAddress
}).Distinct().ToList();
基本上,我想将整个OrderAddress对象存储到Address属性中,但是它不会作为Distinct对象出现。
答案 0 :(得分:1)
您可以使用moreLINQ中的DistinctBy
:
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
city = r.orderAddress.BilltoCity
}).DistinctBy(x => new { x.OrderID, x.subtotal }).ToList();
或GroupBy
,它已成为LINQ的一部分:
var resultsHeader = (from r in o
group r by new { r.OrderIs, r.SubTotal } into g
select new {
g.Key.OrderID,
g.Key.SubTotal,
Address = g.First().orderAddress
}).ToList()
<强>更新强>
或者,您可以在Address
课程上实施Equals
and GetHashCode
,并像现在一样使用Distinct
。