C# Foreach a list<;元组>;井然有序
我有一个用entityframework中的数据填充的列表。此数据按ID排序 我想以一种有序的方式查看这个列表,这样我就可以注册哪个ID已经被处理过了。因为列表中的ID可以出现多次 所以基本上是这样的:C# Foreach a list<;元组>;井然有序,c#,.net,.net-4.5,C#,.net,.net 4.5,我有一个用entityframework中的数据填充的列表。此数据按ID排序 我想以一种有序的方式查看这个列表,这样我就可以注册哪个ID已经被处理过了。因为列表中的ID可以出现多次 所以基本上是这样的: List<int> matchedIDs = new List<string>(); foreach(var item in tupleList) { if(matchedIDs.contains(item.ID)) { // do somethi
List<int> matchedIDs = new List<string>();
foreach(var item in tupleList)
{
if(matchedIDs.contains(item.ID))
{
// do something
}
else
{
// do something else
}
}
List matchedIDs=new List();
foreach(元组列表中的变量项)
{
if(matchedIDs.contains(item.ID))
{
//做点什么
}
其他的
{
//做点别的
}
}
这是可以做到的。那么,每次foreach中出现一个新项目时,该项目是否具有相同的ID
PS:我对匹配副本感兴趣,因为我正在csv文件中插入项目。因此,如果元组中的第一项相同,则不应插入新行,而应使用现有行。如果您已经保留了一个
匹配数据的列表,则不需要对列表进行排序。就像你已经在做的那样去做:
List<int> matchedIDs = new List<string>();
foreach(var item in tupleList)
{
if(matchedIDs.contains(item.ID))
{
// do something
}
else
{
// do something else
matchedIDs.Add(item.ID);
}
}
List matchedIDs=new List();
foreach(元组列表中的变量项)
{
if(matchedIDs.contains(item.ID))
{
//做点什么
}
其他的
{
//做点别的
matchedIDs.Add(item.ID);
}
}
如果您已经保存了匹配数据的列表,则不需要对列表进行排序。就像你已经在做的那样去做:
List<int> matchedIDs = new List<string>();
foreach(var item in tupleList)
{
if(matchedIDs.contains(item.ID))
{
// do something
}
else
{
// do something else
matchedIDs.Add(item.ID);
}
}
List matchedIDs=new List();
foreach(元组列表中的变量项)
{
if(matchedIDs.contains(item.ID))
{
//做点什么
}
其他的
{
//做点别的
matchedIDs.Add(item.ID);
}
}
如果保留已出现ID的列表,则实际上不需要排序。此代码应满足您的需要:
List<int> matchedIDs = new List<int>();
foreach(var item in tupleList)
{
if(matchedIDs.Contains(item.ID))
{
//do something
} else {
matchedIDs.Add(item.ID);
//do something else
}
}
出于您的目的(本质上是一个查找表),HashSet
更适合:
HashSet<int> matchedIDs = new HashSet<int>();
HashSet matchedIDs=new HashSet();
代码的其余部分保持不变
HashSet
更好,因为它的查找时间是O(1),这意味着无论它有多大,它都保持不变。列表的查找时间为O(n),它必须遍历每个元素,以确定您要查找的元素是否在其中。如果保留已出现ID的列表,则不需要进行排序。此代码应满足您的需要:
List<int> matchedIDs = new List<int>();
foreach(var item in tupleList)
{
if(matchedIDs.Contains(item.ID))
{
//do something
} else {
matchedIDs.Add(item.ID);
//do something else
}
}
出于您的目的(本质上是一个查找表),HashSet
更适合:
HashSet<int> matchedIDs = new HashSet<int>();
HashSet matchedIDs=new HashSet();
代码的其余部分保持不变
HashSet
更好,因为它的查找时间是O(1),这意味着无论它有多大,它都保持不变。列表的查找时间为O(n),它必须遍历每个元素以确定您要查找的元素是否在其中。您可能希望按ID进行分组:
foreach(var group in tupleList.GroupBy(x => x.ID))
{
var id = group.Key;
var firstItemWithThisId = group.First();
// iterate the items in the group
foreach (var item in group)
{
// do something with the item
}
// do something
}
或者,如果您只对ID感兴趣,请使用distinct:
foreach(var id in tupleList.Select(x => x.Id).Distinct())
{
// do something with id
}
您可能希望按ID分组:
foreach(var group in tupleList.GroupBy(x => x.ID))
{
var id = group.Key;
var firstItemWithThisId = group.First();
// iterate the items in the group
foreach (var item in group)
{
// do something with the item
}
// do something
}
或者,如果您只对ID感兴趣,请使用distinct:
foreach(var id in tupleList.Select(x => x.Id).Distinct())
{
// do something with id
}
另一种选择重复项的方法是使用GroupBy
:
var grp = tupleList.GroupBy(tl => tl.Id);
var duplicates = grp.Where(g => g.Count() > 1);
var nonDuplicates = grp.Where(g => g.Count() == 1);
另一种选择重复项的方法是使用GroupBy
:
var grp = tupleList.GroupBy(tl => tl.Id);
var duplicates = grp.Where(g => g.Count() > 1);
var nonDuplicates = grp.Where(g => g.Count() == 1);
由于列表中已经有原始项,我们只需要保留已处理的ID列表,并且在处理结束时,您将获得列表中未处理的记录数
List<int> matchedIDs = new List<string>();
foreach (var item in tupleList)
{
if (matchedIDs.contains(item.ID))
{
// do something
}
else
{
if (processed) // process the records and if process then add into list.
{
matchedIDs.Add(item.ID);
}
}
}
List matchedIDs=new List();
foreach(元组列表中的变量项)
{
if(matchedIDs.contains(item.ID))
{
//做点什么
}
其他的
{
if(processed)//处理记录,if process然后添加到列表中。
{
matchedIDs.Add(item.ID);
}
}
}
由于列表中已经有原始项目,我们只需保留已处理的ID列表,处理结束后,您将获得列表中未处理的记录数
List<int> matchedIDs = new List<string>();
foreach (var item in tupleList)
{
if (matchedIDs.contains(item.ID))
{
// do something
}
else
{
if (processed) // process the records and if process then add into list.
{
matchedIDs.Add(item.ID);
}
}
}
List matchedIDs=new List();
foreach(元组列表中的变量项)
{
if(matchedIDs.contains(item.ID))
{
//做点什么
}
其他的
{
if(processed)//处理记录,if process然后添加到列表中。
{
matchedIDs.Add(item.ID);
}
}
}
或使用一个集合var matchedIDs=new HashSet()
然后组合包含检查和添加,如(!matchedIDs.add(item.ID)){…}else{…}
中所示,或者使用一个集合var matchedIDs=new HashSet()
然后组合包含检查和添加,如if(!matchedIDs.add(item.ID)){…}else{…}
。对MatchedId使用哈希集
时,它的性能会好得多。在我看来,您似乎希望对结果进行分组……对MatchedId使用哈希集
时,它的性能会好得多。在我看来,您似乎希望对结果进行分组……因此,这实际上会立即得到ID1,然后再得到ID1,因为它是已订购?它为每个不同的ID创建一个组。具有相同ID的所有项目都在同一个组中。foreach迭代组。如果我的元组包含4个字符串,它会为每个ID执行一次foreach主体。像这样:键入如何访问foreach中的最后3项?正如我试图演示的那样:group.First()提供组中的第一个元组。您可以访问它的所有属性。谢谢您的帮助。这正是我所需要的,所以这实际上会得到ID1,然后马上再得到ID1,因为它是有序的?它会为每个不同的ID创建一个组。具有相同ID的所有项目都在同一个组中。foreach迭代组。如果我的元组包含4个字符串,它会为每个ID执行一次foreach主体。像这样:键入如何访问foreach中的最后3项?正如我试图演示的那样:group.First()提供组中的第一个元组。您可以访问它的所有属性。感谢您的帮助