英文:
Remove adjacent duplicates in List C#
问题
以下是已翻译的内容:
我有一个以下的列表,我只需要删除相邻的重复项。
示例列表
-------------
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "AAA" },
new NewsModel { ID = 3, Name = "BBB" },
new NewsModel { ID = 4, Name = "CCC" },
new NewsModel { ID = 5, Name = "CCC" },
new NewsModel { ID = 6, Name = "AAA" },
new NewsModel { ID = 7, Name = "DDD" },
new NewsModel { ID = 8, Name = "DDD" },
new NewsModel { ID = 9, Name = "CCC" }
};
期望结果
----------------
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "BBB" },
new NewsModel { ID = 3, Name = "CCC" },
new NewsModel { ID = 4, Name = "AAA" },
new NewsModel { ID = 5, Name = "DDD" },
new NewsModel { ID = 6, Name = "CCC" }
};
我尝试过像下面这样,但它没有达到预期的结果。
foreach (var item in NewsModel.ToList())
{
if (NewsModel.Count() >= NewsModel.IndexOf(item) + 1)
{
_nextItem = NewsModel[NewsModel.IndexOf(item) + 1];
if (item.Name == _nextItem.Name)
{
NewsModel.Remove(item);
}
}
}
希望这对你有所帮助。
英文:
I have a below list and i need to remove adjacent duplicate item only.
Sample List
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "AAA" },
new NewsModel { ID = 3, Name = "BBB" },
new NewsModel { ID = 4, Name = "CCC" },
new NewsModel { ID = 5, Name = "CCC" },
new NewsModel { ID = 6, Name = "AAA" },
new NewsModel { ID = 7, Name = "DDD" },
new NewsModel { ID = 8, Name = "DDD" },
new NewsModel { ID = 9, Name = "CCC" }
};
Expected Result
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "BBB" },
new NewsModel { ID = 3, Name = "CCC" },
new NewsModel { ID = 4, Name = "AAA" },
new NewsModel { ID = 5, Name = "DDD" },
new NewsModel { ID = 6, Name = "CCC" }
};
I have tried it like below,But it's not came as expected result..
foreach (var item in NewsModel.ToList())
{
if (NewsModel.Count() >= NewsModel.IndexOf(item) + 1)
{
_nextItem = NewsModel[NewsModel.IndexOf(item) + 1];
if (item.Name == _nextItem.Name)
{
NewsModel.Remove(item);
}
}
}
答案1
得分: 3
这将删除所有相邻的相同条目。
for (int i = 0; i < data.Count-1; i++)
{
if (data[i].Name == data[i+1].Name)
{
data.RemoveAt(i);
i--;
}
}
编辑:
我忽略了你希望在最后更新 ID。在我发送的第一个循环之后再做一个循环,实现这个。
for (int i = 0; i < data.Count; i++)
data[i].ID = i + 1;
英文:
This will remove all adjacent entries that are equal.
for (int i = 0; i < data.Count-1; i++)
{
if (data[i].Name == data[i+1].Name)
{
data.RemoveAt(i);
i--;
}
}
EDIT
I missed that you want the ID's to be updated by the end.
Do another for loop after the first one I sent, that does this
for (int i = 0; i < data.Count; i++)
data[i].ID = i + 1;
答案2
得分: 1
嗯,ID的重新编号有点复杂。
所以,这个代码会这样:
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "AAA" },
new NewsModel { ID = 3, Name = "BBB" },
new NewsModel { ID = 4, Name = "CCC" },
new NewsModel { ID = 5, Name = "CCC" },
new NewsModel { ID = 6, Name = "AAA" },
new NewsModel { ID = 7, Name = "DDD" },
new NewsModel { ID = 8, Name = "DDD" },
new NewsModel { ID = 9, Name = "CCC" }
};
var data2 = new List<NewsModel>();
int iID = 0;
string sPrevious = "";
for (int j = 0; j < data.Count; j++)
{
NewsModel OneNews = data[j];
if (sPrevious != OneNews.Name)
{
iID++;
OneNews.ID = iID;
data2.Add(OneNews);
sPrevious = OneNews.Name;
}
}
foreach (NewsModel OneNews in data2)
Debug.Print($"ID = {OneNews.ID}, Name = {OneNews.Name}");
输出:
ID = 1, Name = AAA
ID = 2, Name = BBB
ID = 3, Name = CCC
ID = 4, Name = AAA
ID = 5, Name = DDD
ID = 6, Name = CCC
如果你要求“原地”从现有列表中删除,或者像上面那样创建一个新列表,这都是一个复杂的选择。
英文:
Hum, the re-numbering of the ID is a bit of monkey wrench.
So, this:
var data = new List<NewsModel>
{
new NewsModel { ID = 1, Name = "AAA" },
new NewsModel { ID = 2, Name = "AAA" },
new NewsModel { ID = 3, Name = "BBB" },
new NewsModel { ID = 4, Name = "CCC" },
new NewsModel { ID = 5, Name = "CCC" },
new NewsModel { ID = 6, Name = "AAA" },
new NewsModel { ID = 7, Name = "DDD" },
new NewsModel { ID = 8, Name = "DDD" },
new NewsModel { ID = 9, Name = "CCC" }
};
var data2 = new List<NewsModel>();
int iID = 0;
string sPrevious = "";
for (int j = 0; j < data.Count;j++)
{
NewsModel OneNews = data[j];
if (sPrevious != OneNews.Name)
{
iID++;
OneNews.ID = iID;
data2.Add(OneNews);
sPrevious= OneNews.Name;
}
}
foreach(NewsModel OneNews in data2)
Debug.Print($"ID = {OneNews.ID}, Name = {OneNews.Name}");
Output:
ID = 1, Name = AAA
ID = 2, Name = BBB
ID = 3, Name = CCC
ID = 4, Name = AAA
ID = 5, Name = DDD
ID = 6, Name = CCC
Tough call if your requiring is to "in place" remove from existing list, or as the above does, and creates a new list.
答案3
得分: 0
这是你提供的内容的中文翻译:
Linq
您可以使用Linq来创建没有相邻重复项的列表,并可以在Select中生成带有索引的新ID。
data = data
.Where((x, i) => i == 0 || x.Name != data[i - 1].Name)
.Select((r, index) => new { ID = index + 1, Name = r.Name }).ToList();
结果
ID | Name |
---|---|
1 | AAA |
2 | BBB |
3 | CCC |
4 | AAA |
5 | DDD |
6 | CCC |
iterator
使用
data = Common.RemoveAdjecentDuplicates(data);
类
public static class Common
{
internal static IEnumerable<NewsModel> RemoveAdjecentDuplicates(IEnumerable<NewsModel> newsModel)
{
using (var iterator = newsModel.GetEnumerator())
{
if (!iterator.MoveNext()) yield break;
var current = iterator.Current;
int Index = 1;
yield return new NewsModel() { Name = current.Name, ID = Index };
while (iterator.MoveNext())
{
if (iterator.Current.Name.Equals(current.Name)) continue;
current = iterator.Current;
Index++;
yield return new NewsModel() { Name = current.Name, ID = Index };
}
}
}
}
英文:
There are several methods. I wrote two methods(linq,iterator)
Linq
You can use Linq to create List without duplicates adjacent and can generate new Id with Index in Select
data = data
.Where((x, i) => i == 0 || x.Name != data[i - 1].Name)
.Select((r, index) => new { ID = index + 1, Name = r.Name }).ToList();
Result
ID | Name |
---|---|
1 | AAA |
2 | BBB |
3 | CCC |
4 | AAA |
5 | DDD |
6 | CCC |
iterator
use
data = Common.RemoveAdjecentDuplicates(data);
Classes
public static class Common
{
internal static IEnumerable<NewsModel> RemoveAdjecentDuplicates(IEnumerable<NewsModel> newsModel)
{
using (var iterator = newsModel.GetEnumerator())
{
if (!iterator.MoveNext()) yield break;
var current = iterator.Current;
int Index = 1;
yield return new NewsModel() { Name = current.Name, ID = Index };
while (iterator.MoveNext())
{
if (iterator.Current.Name.Equals(current.Name)) continue;
current = iterator.Current;
Index++;
yield return new NewsModel() { Name = current.Name, ID = Index };
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论