C# 过滤集合的算法
我有对象国家,它有区域。地区有省份。各省有城市,城市,城市有酒店 我想筛选区域列表,使其仅包含属性userhavebeen设置为true的对象(地区、省、市和酒店) 我将把这个列表绑定到treeview 此算法情况最糟糕的部分,例如: 地区没有userHaveBeenThere==true,所有省份和城市都有,但在一个城市,十家酒店中有一家的userHaveBeenThere为true。 所以我必须向用户展示这个地区,只有一个省,只有一个城市,只有一家酒店 另一件可怕的事情是,我有两个TreeView。首先,我必须显示完整的结构,然后进行过滤,所以在使用诸如remove之类的过滤操作时,我很少担心引用 所以问题是如何过滤这个列表C# 过滤集合的算法,c#,linq,algorithm,optimization,C#,Linq,Algorithm,Optimization,我有对象国家,它有区域。地区有省份。各省有城市,城市,城市有酒店 我想筛选区域列表,使其仅包含属性userhavebeen设置为true的对象(地区、省、市和酒店) 我将把这个列表绑定到treeview 此算法情况最糟糕的部分,例如: 地区没有userHaveBeenThere==true,所有省份和城市都有,但在一个城市,十家酒店中有一家的userHaveBeenThere为true。 所以我必须向用户展示这个地区,只有一个省,只有一个城市,只有一家酒店 另一件可怕的事情是,我有两个TreeV
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
我不想回答如何绑定:)只想回答如何筛选:)
这是我的解决方案:
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);
}
}
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);
}
}
var copiedCountry=CloneObject(_package.Country);
for(int indexOfRegion=0;indexOfRegion0)
{
对于(int indexofprovision=0;indexofprovision0)
{
对于(int indexOfCity=0;indexOfCityh==null);
if(city.ListOfHotels.Where(h=>h.userHaveBeenThere==true).Count()>0)
{
}
其他的
{
if(city.userHaveBeenThere==false)
{
省份。城市列表[索引]=空;
}
}
}
province.ListOfCities.RemoveAll(c=>c==null);
if(province.ListOfCities.Count==0)
{
if(province.userHaveBeenThere==false)
{
region.ListOfProvinces[indexOfProvince]=空;
}
}
}
其他的
{
if(province.userHaveBeenThere==false)
{
region.ListOfProvinces[indexOfProvince]=空;
}
}
}
region.listofProvisions.RemoveAll(p=>p==null);
if(region.listofProvisions.Count==0)
{
if(region.userHaveBeenThere==false)
{
copiedCountry.ListOfRegions[indexOfRegion]=空;
}
}
}
其他的
{
if(region.userHaveBeenThere==false)
{
copiedCountry.ListOfRegions[indexOfRegion]=空;
}
}
copiedCountry.ListOfRegions.RemoveAll(r=>r==null);
}
公共静态T克隆对象(T项)
{
使用(MemoryStream ms=new MemoryStream())
{
BinaryFormatter bf=新的BinaryFormatter(null,
新的StreamingContext(StreamingContextStates.Clone));
尝试
{
bf.序列化(ms,项目);
}
捕获(例外e)
{
投掷;
}
Seek女士(0,SeekOrigin.Begin);
返回(T)bf.反序列化(ms);
}
}
你能为你的所有对象添加一个变量“BeenThereInHere”(或你想要的任何名称)吗?如果他的一个孩子有userHaveBeenThere==true,那么这个变量将被设置为true,这样,你就可以快速确定扫描的位置和可以跳过的位置以节省时间。你能为你的所有对象添加一个变量“BeenThereInHere”(或您想要的任何名称)如果他的一个孩子有userHaveBeenThere==true,则会将其设置为true。这样,您可以快速确定在何处进行扫描,以及在何处可以跳过扫描以节省时间。数据是存储在数据库中还是存储为图形
如果是数据库,您可以设置查询以获取正确的数据
如果它是一个图形,您可以将其设置为双向的,从底部开始(访问过的酒店),然后走到每个根(区域),将父对象插入到一个新对象中。是数据库中的数据或作为图形存储的数据
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);
}
}
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;
}
}
}