我正在做下面的事情,它工作了:
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
city = r.orderAddress.BilltoCity
}).Distinct().ToList();
看city = r.orderAddress。BilltoCity Line,是否有任何方法可以填充整个对象…这样的
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
Address = r.orderAddress
}).Distinct().ToList();
基本上,我想将整个OrderAddress对象存储到Address属性中,但是它不作为Distinct对象。
您可以使用DistinctBy
from moreLINQ:
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
和GetHashCode
,并像现在一样使用Distinct
。