c# - 过滤集合的算法

标签 c# linq algorithm optimization

我有对象国家,其中有地区。地区有省份。省份有城市,城市有酒店。

我想过滤区域列表,只包含属性 userHaveBeenThere 设置为 true(Areas,Provinces,Cities,and hotels) 的对象。

我要把这个列表绑定(bind)到 treeview。

这种算法情况中最糟糕的部分,例如:

地区没有userHaveBeenThere==true,所有省份也没有,所有城市也没有,但在一个城市,十家酒店之一的userHaveBeenThere为真。 所以我必须向用户展示这个区域,只有一个省,只有一个城市,只有一个酒店。

另一个可怕的事情是,我有两个 TreeView 。首先我必须显示完整结构,然后再进行过滤,因此在使用像删除这样的过滤操作时我不太担心引用。

那么问题是如何过滤这个列表?

TreeView1

Region1
-Area1
  -Province1
     -City1
     -City2
         -Hotel1
         -Hotel2
         -Hotel3
  -Province2
     -City3
     -City4
         -Hotel4
         -Hotel5
         -Hotel6
-Area2
Region2
-Area21
  -Province21
     -City21
     -City22
         -Hotel21
         -Hotel22
         -Hotel23
  -Province22
     -City23
     -City24
         -Hotel24
         -Hotel25
         -Hotel26
-Area22

Filtered list
Region1
-Area1
  -Province1
     -City2
       -Hotel3
Region2
-Area21
   -Province22
     -City24
         -Hotel24

我不想回答如何绑定(bind) :) 只回答如何过滤 :)

这是我的解决方案:

 var copiedCountry = CloneObject(_package.Country);


            for (int indexOfRegion = 0; indexOfRegion < copiedCountry.ListOfRegions.Count; indexOfRegion++)
            {
                var region = copiedCountry.ListOfRegions[indexOfRegion];
                if (region.ListOfProvinces.Count > 0)
                {
                    for (int indexOfProvince = 0; indexOfProvince < region.ListOfProvinces.Count; indexOfProvince++)
                    {
                        var province = region.ListOfProvinces[indexOfProvince];
                        if (province.ListOfCities.Count > 0)
                        {
                            for (int indexOfCity = 0; indexOfCity < province.ListOfCities.Count; indexOfCity++)
                            {
                                var city = province.ListOfCities[indexOfCity];
                                int numberOfHotels = city.ListOfHotels.Count;
                                for (int indexOfHotel = 0; indexOfHotel < numberOfHotels; indexOfHotel++)
                                {
                                    var hotel = city.ListOfHotels[indexOfHotel];
                                    if (hotel.userHaveBeenThere  == false)
                                    {
                                        city.ListOfHotels[indexOfHotel] = null;
                                    }
                                }

                                city.ListOfHotels.RemoveAll(h => h == null);

                                if (city.ListOfHotels.Where(h => h.userHaveBeenThere  == true).Count() > 0)
                                {

                                }
                                else
                                {
                                    if (city.userHaveBeenThere  == false)
                                    {
                                        province.ListOfCities[indexOfCity]=null;
                                    }

                                }
                            }

                            province.ListOfCities.RemoveAll(c => c == null);

                            if (province.ListOfCities.Count == 0)
                            {
                                if (province.userHaveBeenThere  == false)
                                {
                                    region.ListOfProvinces[indexOfProvince]=null;
                                }
                            }



                        }
                        else
                        {
                            if (province.userHaveBeenThere  == false)
                            {
                                region.ListOfProvinces[indexOfProvince] = null;
                            }
                        }
                    }

                    region.ListOfProvinces.RemoveAll(p => p == null);

                    if (region.ListOfProvinces.Count == 0)
                    {
                        if (region.userHaveBeenThere  == false)
                        {
                            copiedCountry.ListOfRegions[indexOfRegion]=null;
                        }
                    }
                }
                else
                {
                    if (region.userHaveBeenThere  == false)
                    {
                        copiedCountry.ListOfRegions[indexOfRegion]=null;
                    }
                }

                copiedCountry.ListOfRegions.RemoveAll(r => r == null);
            }


public static T CloneObject<T>(T item)
        {
            using (MemoryStream ms = new MemoryStream())
            {

                BinaryFormatter bf = new BinaryFormatter(null,
                          new StreamingContext(StreamingContextStates.Clone));

                try
                {
                    bf.Serialize(ms, item);
                }
                catch (Exception e)
                {
                    throw;
                }


                ms.Seek(0, SeekOrigin.Begin);


                return (T)bf.Deserialize(ms);
            }
        }

最佳答案

与其使用 bool 值来了解用户是否去过那里,不如考虑使用 bool 值?。 当您想重定向到子对象的 userHasBeenThere 时,将其设置为 null。

这是一个具体的例子,当您将其中一个对象的 userHasBeenThere 设置为 true 时,魔法就会发生。

这是一个模式的例子:

public abstract class AUserTracker
{
    private IEnumerable<AUserTracker> _children;
    public IEnumerable<AUserTracker> Children
    {
        get { return _children; }
        set { _children = value; }
    }

    private bool? _userHasBeenThere;
    public bool UserHasBeenThere
    {
        get
        {
            if (_userHasBeenThere == null)
            {
                _userHasBeenThere = false;
                // Uses OR operator, any child will trigger the show up.
                foreach (AUserTracker child in Children)
                    _userHasBeenThere |= child.UserHasBeenThere;
            }
            return _userHasBeenThere ?? false;
        }
    }
}

这将是您所有对象的基类。然后您可以使用 WPF HierarchicalDatatemplateTreeView 中显示您的对象。

{享受}

关于c# - 过滤集合的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451618/

相关文章:

algorithm - 查找给定预购的树高

algorithm - 为什么交换背包的元素顺序会导致相同的解决方案?

c# - 尝试在 C# 中使用 ODbc 从 MSysObjects 中读取数据,但没有权限错误

c# - WPF XAML 编辑器导致高内存消耗

c# - 使用 Linq 将 xml 转换为类

linq - 是否有与 Linq to XML 等效的 XPath?

c++ - 象限查询

c# - Stackpanel 中的自动调整大小和居中文本 block

c# - 用于控制 OWI-535 机械臂的 API

c# - Linq to SQL DataContext 无法更新数据库中继承对象的枚举字段