我有一個班級清單:
class GroupAssets
{
public string Name { get; set; }
public List<string> Assets { get; set; }
}
List<GroupAssets> GroupList2 = new List<GroupAssets>{
new GroupAssets { Name="Group1", Assets = new List<string>{ "A","B","C","D" }},
new GroupAssets { Name="Group1", Assets = new List<string>{ "A","B","E","F" }},
new GroupAssets { Name="Group3", Assets = new List<string>{ "A","B","H","G" }},
new GroupAssets { Name="Group4", Assets = new List<string>{ "A","I","C","J" }}
};
我想洗掉重復項并得到以下結果:
Group1 => D
Group2 => E,F
Group3 => H,G
Group4 => I,J
Duplicate => A,B,C
謝謝您的幫助
uj5u.com熱心網友回復:
List<GroupAssets> GroupList = new List<GroupAssets>{
new GroupAssets { Name="Group1", Assets = new List<string>{ "A","B","C","D" }},
new GroupAssets { Name="Group1", Assets = new List<string>{ "A","B","E","F" }},
new GroupAssets { Name="Group3", Assets = new List<string>{ "A","B","H","G" }},
new GroupAssets { Name="Group4", Assets = new List<string>{ "A","I","C","J" }}
};
var assetList = new Dictionary<string,int>();
foreach (var g in GroupList.Select(x=> x.Assets)) {
g.ForEach(x=> {
if (!assetList.ContainsKey(x)) assetList.Add(x,1);
else assetList[x] ;
});
}
var nonUnique = assetList.Where(x=> x.Value > 1).Select(x=> x.Key).ToList();
nonUnique.ForEach(x=> { Console.WriteLine(x); });
如果您想知道重復的總量,替代解決方案
uj5u.com熱心網友回復:
我假設您犯了一個錯誤,并且屬性GroupAssets.Assets包含資產串列 ( new List<string>() {"A", "B"}),而不是逗號分隔的字串串列,串列中只有一個字串 ( new List<string>() {"A,B"})。
首先,您必須弄清楚哪些是重復項。您可以按字串“A”到“J”之一對專案進行分組,值int是該鍵在所有串列中出現的次數。我們從另一個 Stack Overflow question中獲取代碼,用一個增強,SelectMany因為我們想將許多串列扁平化為一個。
var assetCount = GroupList
.SelectMany(x => x.Assets)
.GroupBy(x => x)
.Select(s => new { Asset = s.Key, Count = s.Count() });
然后我們制作重復串列和具有唯一資產的組串列:
var duplicates = assetCount.Where(x => x.Count > 1).Select(x => x.Asset).ToList();
var uniqueAssetsGroupList = GroupList
.Select(x => new GroupAssets() { Name = x.Name, Assets = x.Assets.Except(duplicates).ToList() });
foreach (var group in uniqueAssetsGroupList)
Console.WriteLine(string.Format("{0} => {1}", group.Name, string.Join(",", group.Assets)));
Console.WriteLine("Duplicate => {0}", string.Join(",", duplicates));
uj5u.com熱心網友回復:
假設你有 class GroupAssets { public String Name { get; 放; } 公共 IList 資產 { 獲取;放; } }
List<GroupAssets> GroupList = new List<GroupAssets>{
new GroupAssets { Name="Group1", Assets = new List<string>{ "A" ,"B", "C", "D" }},
new GroupAssets { Name="Group2", Assets = new List<string>{ "A" ,"B", "E", "F" }},
new GroupAssets { Name="Group3", Assets = new List<string>{ "A" ,"B", "H", "G" }},
new GroupAssets { Name="Group4", Assets = new List<string>{ "A" ,"I", "C", "J" }},
};
請注意,每個Asset都有4 個專案(不是1 個)你可以放
代碼:
HashSet<string> duplicates = new HashSet<string>();
HashSet<string> all = new HashSet<string>();
foreach (var item in GroupList)
foreach (var asset in item.Assets)
if (!all.Add(asset)) // duplicate if all contains the asset
duplicates.Add(asset);
// removing duplicates from each Asset
foreach (var item in GroupList)
item.Assets.RemoveAll(item => duplicates.Contains(item));
我們來看一下:
string report = string.Join(Environment.NewLine, GroupList
.Select(item => $"{item.Name} => {string.Join(", ", item.Assets)}"));
Console.WriteLine(report);
Console.WriteLine("Duplicate => {string.Join(", ", duplicates)}");
結果:
Group1 => D
Group2 => E, F
Group3 => H, G
Group4 => I, J
Duplicate => A, B, C
但是,如果每個都Assets包含 1 個逗號分隔項,則應添加Splitand Join:
HashSet<string> duplicates = new HashSet<string>();
HashSet<string> all = new HashSet<string>();
foreach (var item in GroupList)
foreach (var asset in item.Assets.SelectMany(list => list.Split(',')))
if (!all.Add(asset))
duplicates.Add(asset);
foreach (var item in GroupList) {
item.Assets = item
.Assets
.Select(asset => asset.Split(',').Where(c => !duplicates.Contains(c)))
.Where(asset => asset.Any())
.Select(asset => string.Join(",", asset))
.ToList();
}
uj5u.com熱心網友回復:
找到唯一的重復項,然后使用 except 從資產串列中洗掉重復項
[Fact]
public void TestRemoveDuplicate()
{
List<GroupAssets> GroupList = new List<GroupAssets>{
new GroupAssets { Name="Group1", Assets = new List<string>{ "A" ,"B", "C", "D" }},
new GroupAssets { Name="Group2", Assets = new List<string>{ "A" ,"B", "E", "F" }},
new GroupAssets { Name="Group3", Assets = new List<string>{ "A" ,"B", "H", "G" }},
new GroupAssets { Name="Group4", Assets = new List<string>{ "A" ,"I", "C", "J" }},
};
IList<String> duplicates = new List<String>();
foreach (var item in GroupList)
{
foreach (var element in item.Assets)
{
if (GroupList.Where(e =>e.Name!=item.Name && e.Assets.Contains(element)).Any())
{
if (duplicates.Contains(element) == false) { duplicates.Add(element); }
}
}
}
foreach (var item in GroupList)
{
item.Assets = item.Assets.Except(duplicates).ToList();
string result = "";
foreach (var element in item.Assets)
{
result = element " ";
}
_output.WriteLine($"Name: {item.Name} Asset: {result}");
}
Assert.True(duplicates.Count() > 0);
}
輸出:
Name: Group1 Asset: D
Name: Group2 Asset: E F
Name: Group3 Asset: H G
Name: Group4 Asset: I J
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411341.html
標籤:
下一篇:Linq中的日期過濾
