[英]How can i split and get distinct words in a list?
在刪除重復項之前,我將使用SelectMany
來“拉平”列表:
products.SelectMany(c => c.Category.Split('~'))
.Distinct()
您可以使用SelectMany
展平集合:
products.SelectMany(p => p.Category.Split('~')).Distinct().ToList();
親密無間,您只需要展平集合即可通過SelectMany()
調用提取每個分組的各個項目:
// The SelectMany will map the results of each of your Split() calls
// into a single collection (instead of multiple)
var test = products.SelectMany(p => p.Category.Split('~'))
.Distinct()
.ToList();
您可以在這里和下面看到一個完整的工作示例 :
// Example input
var input = new string[] { "SHOES","SHOES~SHOCKS","SHOES~SHOCKS~ULTRA SOCKS" };
// Get your results (yields ["SHOES","SHOCKS","ULTRA SOCKS"])
var output = input.SelectMany(p => p.Split('~'))
.Distinct()
.ToList();
通過使用SelectMany()
將此list of strings
列表list of strings
合並到一個列表中,然后向列表中添加另一個Distinct。
var test = from c in products select c.Category.Split('~').Distinct().ToList().SelectMany(x => x).Distinct().ToList();
這是您在查詢語法中的處理方式。
var test = (from p in products
from item in p.Category.Split('~')
select item).Distinct().ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.