[英]Compare two Lists for duplicates
我有兩個清單:
public static List<string> SalesAuthorityListFromFiles = new List<string>();
public static List<(string UserName, string LegalCompany, string SalesAuthorities)> ListOfSalesAuthorities = new List<(string UserName, string LegalCompany, string SalesAuthorities)>();
我正在嘗試建立一個新列表以獲取“已刪除的銷售權限”:
var SalesAuthorityThatNotMatch = SalesAuthorityListFromFiles.Where(x => !ListOfSalesAuthorities.Contains(x)).ToList();
我收到一個指向x
的錯誤,我明白為什么,但我不確定如何解決它:
嚴重性代碼說明項目文件行抑制狀態錯誤 CS1503 參數 1:無法從“字符串”轉換為“(字符串用戶名、字符串 LegalCompany、字符串 SalesAuthorities)”
我需要將string UserName, string LegalCompany, string SalesAuthorities
到一個字符串,以便與SalesAuthorityListFromFiles
值進行比較:
string.Format(@"{0} / {1} : {2}.00 €", UserName, LegalCompany, SalesAuthorities)
但是我如何才能與SalesAuthorityListFromFiles
值進行比較?
將您的Contains
更改為Any
然后您可以在比較之前格式化每個元組:
var salesAuthorityThatNotMatch = SalesAuthorityListFromFiles
.Where(x => !ListOfSalesAuthorities
.Any(y => $"{y.UserName} / {y.LegalCompany} : {y.SalesAuthorities}.00 €" == x))
.ToList();
您也可以創建格式化元組的HashSet<string>
,這將使用更多內存,但會提高 LINQ 查詢的效率:
var salesAuthoritiesFormatted = ListOfSalesAuthorities
.Select(y => $"{y.UserName} / {y.LegalCompany} : {y.SalesAuthorities}.00 €")
.ToHashSet();
然后HashSet<string>.Contains
將以O(1)
時間復雜度工作:
var salesAuthorityThatNotMatch = SalesAuthorityListFromFiles
.Where(x => !salesAuthoritiesFormatted.Contains(x))
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.