C# 在逗号分隔的字符串中查找重复项的索引位置
我的问题比我想象的要复杂得多,我已经把原来的问题删掉了。。。因此,我可能会发布多个问题,这取决于我如何处理这个问题 无论如何,回到问题上来。我需要找到包含csv数据的字符串中重复项的索引位置。比如说, 名字,姓氏,地址,地址,地址,城市,邮政编码,邮政编码,国家 如您所见,地址是重复的,我需要找出每个重复的索引,假设第一个索引位置从0开始 如果你有更好的建议,让我知道如何做,但假设可以做到,我们可以用DICTIONARY> 因此,如果我必须编写此代码,您将:C# 在逗号分隔的字符串中查找重复项的索引位置,c#,linq,C#,Linq,我的问题比我想象的要复杂得多,我已经把原来的问题删掉了。。。因此,我可能会发布多个问题,这取决于我如何处理这个问题 无论如何,回到问题上来。我需要找到包含csv数据的字符串中重复项的索引位置。比如说, 名字,姓氏,地址,地址,地址,城市,邮政编码,邮政编码,国家 如您所见,地址是重复的,我需要找出每个重复的索引,假设第一个索引位置从0开始 如果你有更好的建议,让我知道如何做,但假设可以做到,我们可以用DICTIONARY> 因此,如果我必须编写此代码,您将: duplicateIndexList
duplicateIndexList.Add(2);
duplicateIndexList.Add(3);
duplicateIndexList.Add(4);
myDuplicateList.Add("Address", duplicateIndexList);
duplicateIndexList.Add(6);
duplicateIndexList.Add(7);
myDuplicateList.Add("PostCode", duplicateIndexList);
显然,我不想这样做,但使用Linq来实现上述目标是否可行?我可能会编写一个函数来实现这一点,但我喜欢看到如何使用Linq来完成事情
以防你好奇我为什么要这么做?简而言之,我有一个xml定义,用于将csv字段映射到数据库字段,我想首先找出是否有重复的列,然后我想附加实际csv行中的相关值,即地址=地址(2)+地址(3)+地址(4),邮政编码=邮政编码(6)+邮政编码(7)
下一部分将是如何从上面定义的csv字符串中删除所有相关值,这些值基于我添加了实际值后找到的索引,但这将是下一部分
谢谢
T
更新:
这是我想要的函数,但正如我所说,linq会很好。请注意,在这个函数中,我使用的是列表而不是逗号分隔的字符串,因为我还没有将该列表转换为csv字符串
Dictionary<string, List<int>> duplicateEntries = new Dictionary<string, List<int>>();
int indexPosition = 0;
foreach (string fieldName in Mapping.Select(m=>m.FieldName))
{
string key = fieldName.ToUpper();
if (duplicateEntries.ContainsKey(key))
{
List<int> indexes = duplicateEntries[fieldName];
indexes.Add(indexPosition);
duplicateEntries[key] = indexes;
indexes = null;
}
else
{
duplicateEntries.Add(key, new List<int>() { indexPosition });
}
indexPosition += 1;
}
Dictionary duplicateEntries=new Dictionary();
int-indexPosition=0;
foreach(映射中的字符串fieldName.Select(m=>m.fieldName))
{
string key=fieldName.ToUpper();
if(重复条目。容器(键))
{
列表索引=重复项[fieldName];
index.Add(indexPosition);
重复项[键]=索引;
索引=null;
}
其他的
{
Add(key,new List(){indexPosition});
}
indexPosition+=1;
}
也许这将有助于澄清我试图实现的目标。您需要执行以下操作:
。在结果数组上选择
,以投影对象的新IEnumerable
,该对象包含数组中项目的索引以及值ToLookup
或GroupBy
和ToDictionary
按列值对结果进行分组var lookup = columnArray
.Select((c, i) => new { Value = c, Index = i })
.ToLookup(o => o.Value, o => o.Index);
List<int> addressIndexes = lookup["Address"].ToList(); // 2, 3, 4
var lookup=columnArray
.Select((c,i)=>new{Value=c,Index=i})
.ToLookup(o=>o.Value,o=>o.Index);
列表地址索引=查找[“地址”].ToList();//2, 3, 4
或者如果您想创建词典
:
Dictionary Dictionary=columnArray
.Select((c,i)=>new{Value=c,Index=i})
.GroupBy(o=>o.Value,o=>o.Index)
.ToDictionary(grp=>grp.Key,grp=>grp.ToList());
列表地址索引=字典[“地址”];//2, 3, 4
编辑
(回应最新问题)
这应该起作用:
Dictionary<string, List<int>> duplicateEntries = Mapping
.Select((m, i) => new { Value = m.FieldName, Index = i })
.GroupBy(o => o.Value, o => o.Index)
.ToDictionary(grp => grp.Key, grp => grp.ToList());
字典重复项=映射
.Select((m,i)=>new{Value=m.FieldName,Index=i})
.GroupBy(o=>o.Value,o=>o.Index)
.ToDictionary(grp=>grp.Key,grp=>grp.ToList());
您需要执行以下操作:
。在结果数组上选择
,以投影对象的新IEnumerable
,该对象包含数组中项目的索引以及值ToLookup
或GroupBy
和ToDictionary
按列值对结果进行分组var lookup = columnArray
.Select((c, i) => new { Value = c, Index = i })
.ToLookup(o => o.Value, o => o.Index);
List<int> addressIndexes = lookup["Address"].ToList(); // 2, 3, 4
var lookup=columnArray
.Select((c,i)=>new{Value=c,Index=i})
.ToLookup(o=>o.Value,o=>o.Index);
列表地址索引=查找[“地址”].ToList();//2, 3, 4
或者如果您想创建词典
:
Dictionary Dictionary=columnArray
.Select((c,i)=>new{Value=c,Index=i})
.GroupBy(o=>o.Value,o=>o.Index)
.ToDictionary(grp=>grp.Key,grp=>grp.ToList());
列表地址索引=字典[“地址”];//2, 3, 4
编辑
(回应最新问题)
这应该起作用:
Dictionary<string, List<int>> duplicateEntries = Mapping
.Select((m, i) => new { Value = m.FieldName, Index = i })
.GroupBy(o => o.Value, o => o.Index)
.ToDictionary(grp => grp.Key, grp => grp.ToList());
字典重复项=映射
.Select((m,i)=>new{Value=m.FieldName,Index=i})
.GroupBy(o=>o.Value,o=>o.Index)
.ToDictionary(grp=>grp.Key,grp=>grp.ToList());
您可以执行以下操作:
int count = 0;
var numbered_collection =
from line in File.ReadAllLines("your_csv_name.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = count++,
First_Field = parts[0],
Second_Field = parts[1], // rinse and repeat
};
这将为您提供每个项目的Id。(并跳过包含标题的第一行)。如果要自动将名称从第一行映射到字段,可以将其放入方法中)
从那里,您可以执行以下操作:
var duplicates = (from items in numbered_collection
group items by items.First_Field into g
select g)
.Where(g => g.Count() > 1);
现在,您已经拥有了所有组,其中实际上有重复项,您可以从对象中获取“Id”,以知道哪一个是重复项 您可以执行以下操作:
int count = 0;
var numbered_collection =
from line in File.ReadAllLines("your_csv_name.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = count++,
First_Field = parts[0],
Second_Field = parts[1], // rinse and repeat
};
这将为您提供每个项目的Id。(并跳过包含标题的第一行)。如果要自动将名称从第一行映射到字段,可以将其放入方法中)
从那里,您可以执行以下操作:
var duplicates = (from items in numbered_collection
group items by items.First_Field into g
select g)
.Where(g => g.Count() > 1);
现在,您已经拥有了所有组,其中实际上有重复项,您可以从对象中获取“Id”,以知道哪一个是重复项 嗨,如果我有一个列表而不是逗号分隔的字符串,你介意扩展这个吗?我会活着看到我的选择。我刚刚完成了一个小函数的编写,并将发布代码,但在这个函数中,它使用的是列表。谢谢,只要用你的列表替换
columnArray
(只要它还可以