简体   繁体   English

List.Clear()无法正常工作

[英]List.Clear() is not working

private Map GetMinMaxDistanceShcoolData(Map MonitorsData)
{
    var tempData = MonitorsData.MonitorsDataList;

    if (tempData != null)
    {
        foreach (var monitor in tempData)
        {
            foreach (var tehsil in monitor.Tehsils)
            {
                var ordered = tehsil.Schools.OrderBy(x => x.Distance).ToList();
                var min = ordered.FirstOrDefault();
                var max = ordered.LastOrDefault();

                ordered.ToList().Clear();

                tehsil.Schools.ToList().Add(min);
                tehsil.Schools.ToList().Add(max);
            }
        }
    }

    return null;
}

Before and After clearing the list the count of the list is same. 清除列表之前和之后,列表的计数相同。 Even when I am using the Remove() instead of Clear(), it is also not removing the list. 即使当我使用Remove()而不是Clear()时,它也不会删除列表。 Also the Add(min) and Add(max) is not working. 另外,Add(min)和Add(max)不起作用。

It's working just fine, you're just calling one many ToList 工作正常,您只需要调用多个ToList

ordered is a List, then you call ToList (creating a new List), then you clear that last List (not touching the first one). 命令是一个列表,然后调用ToList(创建一个新列表),然后清除最后一个列表(不触摸第一个)。

What you wrote : 你写的是:

ordered.ToList().Clear();

Is the same as 是相同的

var newlist = ordered.ToList();
newlist.Clear(); // of course this doesn't clear ordered

What you need to do is simply 您需要做的就是

ordered.Clear();

You are doing the wrong thing. 您做错了事。 ToList creates a new List, therefore Ordered is a new list, and you clear that, but not the orginal data. ToList将创建一个新列表,因此Ordered是一个新列表,您可以清除该列表,但不能清除原始数据。

 ordered.ToList().Clear();

Also ordered is already a list, calling ToList is redundant ordered已经是列表,调用ToList是多余的

I think you meant to do this: 我认为您打算这样做:

var ordered = tehsil.Schools.OrderBy(x => x.Distance).ToList();
var min = ordered.FirstOrDefault();
var max = ordered.LastOrDefault();

tehsil.Schools.Clear();

tehsil.Schools.Add(min);
tehsil.Schools.Add(max);

Remove the ToList() call before Clear() Clear()之前删除ToList()调用

var tempData = MonitorsData.MonitorsDataList;

if (tempData != null)
{
    foreach (var monitor in tempData)
    {
        foreach (var tehsil in monitor.Tehsils)
        {
            var ordered = tehsil.Schools.OrderBy(x => .Distance).ToList();
            var min = ordered.FirstOrDefault();
            var max = ordered.LastOrDefault();

            ordered.Clear();

            tehsil.Schools.ToList().Add(min);
            tehsil.Schools.ToList().Add(max);
        }
    }
}

Replace ordered.ToList().Clear(); 替换ordered.ToList().Clear(); with ordered.Clear(); ordered.Clear();

You're clearing the list created by .ToList() 您正在清除由.ToList()创建的列表

ordered.ToList().Clear();
// .ToList() - Creates a new list.
// .Clear() - Clears the list that was created.

To clear a list use: 要清除列表,请使用:

ordered.Clear();
// .Clear() - Clears the list called ordered.

hi you have a few issues here, try something like this. 您好,您在这里遇到一些问题,请尝试类似的方法。

foreach (var tehsil in monitor.Tehsils)
{
    var ordered = tehsil.Schools.OrderBy(x => x.Distance).ToList();
    if(!ordered.Any || ordered.Count < 3) continue;
    var maxmin = new List<tehsil.Schools> { ordered.FirstOrDefault(), ordered.LastOrDefault() }
    tehsil.Schools = maxmin;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM