我是LINQ的新手,並且有一個基本查詢。Linq鏈接SELECT和DISTINCT運算符
說我有客戶大名單的對象
List<Customer> c = null;
c = //Fetch from DB - resulting into 1000+ non-unique Customers;
如果我轉換列表轉換成說,另一個類的列表 - 由於缺乏一個更好的名字 - CustomerEntity,然後挑選出不同的人作爲如下:
var ce = c.Select(cust => new CustomerEntity()
{
CustomerID = cust.CustID,
CustomerName = cust.CustName
}).Distinct(new CustomerEntityComparer()).ToList();
CustomerEntityComparer只是一個根據CustomerID比較2個CustomerEntity對象的類。 我的查詢是: 如果選擇和區別被鏈接,會導致在列表上多次迭代?
感謝
維卡斯
什麼問題?你做了什麼來調查它? – erikkallen
簡單的答案是:不。但是你可以在數據庫端做這些轉換('SELECT DISTINCT')... –
如果你想了解更多關於LINQ(對象)的內部工作方式,我推薦http://msmvps.com/blogs/jon_skeet/ archive/2011/02/23/reimplementing-linq-to-objects-part-45-conclusion-and-list-of-posts.aspx其中Jon Skeet重新創建了LINQ並解釋了所有涉及的原理(例如,迭代)。簡單的答案是,它只會迭代原始列表一次。 – Chris