如何过滤列表?

发布于 2024-08-10 23:48:04 字数 878 浏览 5 评论 0原文

这是一种应该将已分配的用户从列表中取出的方法 并将未分配的保留在列表中。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久伴你 2024-08-17 23:48:04

如果 VListList,那么您可以这样做:

profileList.RemoveAll(profile => GuidList.Contains(profile.UserId));

如果性能是一个问题并且有很多 Guid 需要删除,那么您可以GuidList 是一个HashSet

编辑基于评论:如果您不想修改原始列表,请执行以下操作:

var filtered = new VList<VW_profiles>(
    profileList.Where(profile => !GuidList.Contains(profile.UserId)));

编辑如果您不使用列表< /code>,这里是一种可以在实现 IList 的可调整大小的列表上使用的方法,也可以在数组 (T[]) 上使用。通过仅从列表末尾删除项目,对于 IList 的大多数实现来说,O(n²) 算法将变为 O(n)。

public static void RemoveAll<T>(this IList<T> list, Predicate<T> match)
{
    if (list == null)
        throw new ArgumentNullException("list");
    if (match == null)
        throw new ArgumentNullException("match");
    if (list is T[])
        throw new ArgumentException("Arrays cannot be resized.");

    // early out
    if (list.Count == 0)
        return;

    // List<T> provides special handling
    List<T> genericList = list as List<T>;
    if (genericList != null)
    {
        genericList.RemoveAll(match);
        return;
    }

    int targetIndex = 0;
    for (int i = 0; i < list.Count; i++)
    {
        if (!match(list[i]) && targetIndex != i)
        {
            list[targetIndex] = list[i];
            targetIndex++;
        }
    }

    // Unfortunately IList<T> doesn't have RemoveRange either
    for (int i = list.Count - 1; i >= targetIndex; i--)
    {
        list.RemoveAt(i);
    }
}

public static void RemoveAll<T>(ref T[] array, Predicate<T> match)
{
    if (array == null)
        throw new ArgumentNullException("array");
    if (match == null)
        throw new ArgumentNullException("match");

    int targetIndex = 0;
    for (int i = 0; i < array.Length; i++)
    {
        if (!match(array[i]) && targetIndex != i)
        {
            array[targetIndex] = array[i];
            targetIndex++;
        }
    }

    if (targetIndex != array.Length)
    {
        Array.Resize(ref array, targetIndex);
    }
}

If VList<T> is a List<T>, then you can do this:

profileList.RemoveAll(profile => GuidList.Contains(profile.UserId));

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:

var filtered = new VList<VW_profiles>(
    profileList.Where(profile => !GuidList.Contains(profile.UserId)));

Edit If you are not using a List<T>, here is a method you can use on resizable lists implementing IList<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 of IList<T>.

public static void RemoveAll<T>(this IList<T> list, Predicate<T> match)
{
    if (list == null)
        throw new ArgumentNullException("list");
    if (match == null)
        throw new ArgumentNullException("match");
    if (list is T[])
        throw new ArgumentException("Arrays cannot be resized.");

    // early out
    if (list.Count == 0)
        return;

    // List<T> provides special handling
    List<T> genericList = list as List<T>;
    if (genericList != null)
    {
        genericList.RemoveAll(match);
        return;
    }

    int targetIndex = 0;
    for (int i = 0; i < list.Count; i++)
    {
        if (!match(list[i]) && targetIndex != i)
        {
            list[targetIndex] = list[i];
            targetIndex++;
        }
    }

    // Unfortunately IList<T> doesn't have RemoveRange either
    for (int i = list.Count - 1; i >= targetIndex; i--)
    {
        list.RemoveAt(i);
    }
}

public static void RemoveAll<T>(ref T[] array, Predicate<T> match)
{
    if (array == null)
        throw new ArgumentNullException("array");
    if (match == null)
        throw new ArgumentNullException("match");

    int targetIndex = 0;
    for (int i = 0; i < array.Length; i++)
    {
        if (!match(array[i]) && targetIndex != i)
        {
            array[targetIndex] = array[i];
            targetIndex++;
        }
    }

    if (targetIndex != array.Length)
    {
        Array.Resize(ref array, targetIndex);
    }
}
浴红衣 2024-08-17 23:48:04

您的问题出在这段代码中:

foreach(Guid userId in GuidList)
{
    if(profile.UserId != userId)
    {
        sortedList.Add(profile)
    }
}

它应该更像是:

bool inList = false;
foreach(Guid userId in GuidList)
{
    if(profile.UserId == userId)
    {
        inList = true;
    }
}
if (!inList)
    sortedList.Add(profile)

或者,更多的 LINQ 风格:

bool inList = GuidList.Any(x => x == profile.UserId);
if (!inList)
    sortedList.Add(profile)

您当前的代码更像是:

GuidList.Where(x => x != profile.UserId)
        .Foreach(x => sortedList.Add(x));

我认为这不是您想要的:)

Your problem is in this code:

foreach(Guid userId in GuidList)
{
    if(profile.UserId != userId)
    {
        sortedList.Add(profile)
    }
}

It should be more like:

bool inList = false;
foreach(Guid userId in GuidList)
{
    if(profile.UserId == userId)
    {
        inList = true;
    }
}
if (!inList)
    sortedList.Add(profile)

or, the more LINQ-style:

bool inList = GuidList.Any(x => x == profile.UserId);
if (!inList)
    sortedList.Add(profile)

your current code is more like:

GuidList.Where(x => x != profile.UserId)
        .Foreach(x => sortedList.Add(x));

which I'm thinking isn't what you want :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文