簡體   English   中英

使用LINQ選擇具有不同列值的行

[英]Selecting rows with distinct column values using LINQ

我有一個SharePoint列表項的通用列表(列出員工)

SharePoint列表包含兩列,“員工姓名”和“員工指定”

我想獲得一個包含不同“Employee Designation”列值的對象List。

如何使用LINQ執行此操作?

這對我沒有用:

 var designations = from SPListItem employee in employees
                    select new {Designation = employee["Employee Designation"].ToString().Distinct()};

如果只檢索該列的字符串值,則可以通過選擇字符串(而不是包含字符串屬性的對象)來避免編寫相等比較器。

var designations = (from SPListItem employee in employees
                    select employee["Employee Designation"].ToString())
                   .Distinct()
                   .ToList();

您必須在序列上使用Distinct方法,並且您需要提供IEqualityComparer<T>的自定義實現,因此您需要為要創建的對象提供顯式類。

var designations = (from SPListItem employee in employees
                   select new 
                   {
                     Designation = employee["Employee Designation"].ToString()
                   }).Distinct(comparer);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM