如何过滤列表?
这是一种应该将已分配的用户从列表中取出的方法 并将未分配的保留在列表中。 GuidList 添加了 userId 单击按钮即可找到它。 profileList 用于填充 gridView。
这是代码:
private VList<VW_profiles> FilterAssigned(VList<VW_profiles> profileList)
{
VList<VW_profiles> sortedList = new VList<VW_profiles>();
foreach(VW_profiles profile in profileList)
{
if(GuidList.Count > 0)
{
foreach(Guid userId in GuidList)
{
if(profile.UserId != userId)
{
sortedList.Add(profile)
}
}
}
else
{
sortedList = profileList;
}
}
return sortedList;
}
现在这是我的问题。一切似乎都运转良好,直到 profileList 中的所有项目也已 添加到 GuidList。然后而不是做否定 两个 Guid ID,我们开始再次添加每个人。有没有人对如何做到这一点有任何建议,这是一种更有效的方法,并且可以在我们取出所有内容后避免添加。
谢谢!
This is a method that should take already assigned users out of a list
and keep non-assigned ones in a list. The GuidList has the userId added
to it on a button click. The profileList is used to populate a gridView.
Here is the code:
private VList<VW_profiles> FilterAssigned(VList<VW_profiles> profileList)
{
VList<VW_profiles> sortedList = new VList<VW_profiles>();
foreach(VW_profiles profile in profileList)
{
if(GuidList.Count > 0)
{
foreach(Guid userId in GuidList)
{
if(profile.UserId != userId)
{
sortedList.Add(profile)
}
}
}
else
{
sortedList = profileList;
}
}
return sortedList;
}
Now here's my problem. Everythings seems to work well up until
all of the items in the profileList have also been
added to the GuidList. Then instead of doing a negate on
the two Guid ID's, we start adding everyone in again. Does anyone have any suggestions on how to do this is a more effecient way and to avoid the adding in once we've taken everything out.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
VList
是List
,那么您可以这样做:如果性能是一个问题并且有很多 Guid 需要删除,那么您可以GuidList 是一个
HashSet
。编辑基于评论:如果您不想修改原始列表,请执行以下操作:
编辑如果您不使用
列表< /code>,这里是一种可以在实现
IList
的可调整大小的列表上使用的方法,也可以在数组 (T[]
) 上使用。通过仅从列表末尾删除项目,对于IList
的大多数实现来说,O(n²) 算法将变为 O(n)。If
VList<T>
is aList<T>
, then you can do this:If performance is an issue and there are LOTS of Guids to remove, then you can make GuidList a
HashSet<Guid>
.Edit Based on comments: If you don't want to modify the original list, then do this:
Edit If you are not using a
List<T>
, here is a method you can use on resizable lists implementingIList<T>
and one you can use on arrays (T[]
). By only ever removing items from the end of the list, what would be an O(n²) algorithm will be O(n) for most implementations ofIList<T>
.您的问题出在这段代码中:
它应该更像是:
或者,更多的 LINQ 风格:
您当前的代码更像是:
我认为这不是您想要的:)
Your problem is in this code:
It should be more like:
or, the more LINQ-style:
your current code is more like:
which I'm thinking isn't what you want :)