c# Linq如何用对象的数据过滤列表并将结果存储在另一个列表中?



对象的结构

int capacityNeeded, string preferedneighbor, string[] resourcesNeeded(对象属性为IEnumerable <string)>

"Centro">

6日,新的[]{"wi-fi","tv"}

我的办公室列表结构

字符串LocationName,字符串的名字,int MaxCapacity,IEnumerable AvailableResources

我的位置列表结构字符串的名字,字符串附近

我需要将该对象与包含多个办公室的列表进行比较,并通过返回最近的一个来返回包含偏好的办公室,我的列表不一定包含相同的资源,包含不同的功能,我需要返回最近的一个,我的办公室列表没有邻居,但它有位置的名称位置列表有邻居,我理解要实现的逻辑,但是我真正需要的是如何编写代码,例如我可以获得包含必要的能力的办公室,以及包含首选附近的位置,但我不知道如何得到包含资源的办公室,并考虑可能我需要和其他的资源,例如我的偏好是{"wi-fi","tv"和我有一个办公室,{"wi-fi","tv"我应该把它还给你,因为它符合我的喜好。

我补充说,preferedneighbor和resourcesNeeded都可以为null

我写的一小段代码

public IEnumerable<IOffice> GetOfficeSuggestion(SuggestionRequest suggestionRequest)
{
var resources = suggestionRequest.ResourcesNeeded;
if (resources.Count() == 0 
|| string.IsNullOrEmpty(suggestionRequest.PreferedNeigborHood))
{
var officeSuggestion = offices
.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded)
.OrderBy(o => o.MaxCapacity);
foreach (var office in officeSuggestion)
{
var list = new OfficeDto
{
Name = office.Name,
LocationName = office.LocationName,
MaxCapacity = office.MaxCapacity,
AvailableResources = office.AvailableResources
};
_suggestionsOffice.Add(list);
}
return _suggestionsOffice;
}
else
{
var officeSuggestion = offices
.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded)
.OrderBy(o => o.MaxCapacity);
foreach (var office in officeSuggestion)
{
var list = new OfficeDto
{
Name = office.Name,
LocationName = office.LocationName,
MaxCapacity = office.MaxCapacity,
AvailableResources = office.AvailableResources
};
_suggestionsOffice.Add(list);
}
return _suggestionsOffice;
}
}

让这个测试项目检查一下链接

public class Program
{
public static List<Office> offices = new List<Office>(){
new Office{
LocationName = "test",
MaxCapacity = 6,
AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
},
new Office{
LocationName = "test",
MaxCapacity = 8,
AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
}
,
new Office{
LocationName = "test",
MaxCapacity = 3,
AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
},
new Office{
LocationName = "test",
MaxCapacity = 2,
AvailableResources = new[]{"wi-fi", "Smth"}
},
new Office{
LocationName = "test",
MaxCapacity = 9,
AvailableResources = new[]{"test","tv", "Smth"}
},
new Office{
LocationName = "test",
MaxCapacity = 7,
AvailableResources = new[]{"wi-fi", "tv","Smth"}
},
new Office{
LocationName = "test",
MaxCapacity = 8,
AvailableResources = new List<string>()
}
};

public static void Main()
{
var officeSuggestion = GetOfficeSuggestion(new Request{
CapacityNeeded= 6, PreferedNeigborHood="not used", ResourcesNeeded= new[] { "wi-fi", "tv" } });

Console.WriteLine(officeSuggestion.ToList().Count.ToString());
Console.ReadLine();
}

public static IEnumerable<Office> GetOfficeSuggestion(Request suggestionRequest)
{
var resources = suggestionRequest.ResourcesNeeded;
var enumerable = resources.ToList();
//Console.WriteLine(enumerable.ToList().Count.ToString());
if (!enumerable.Any() || string.IsNullOrEmpty(suggestionRequest.PreferedNeigborHood))
{
var officeSuggestion = offices.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded).OrderBy(o => o.MaxCapacity);

return officeSuggestion;
}
else
{
var officeSuggestion = offices.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded  &&  !enumerable.Except(x.AvailableResources).Any())
.OrderBy(o =>o.AvailableResources.Except(enumerable).Count()); 
return officeSuggestion;
}
}

}
public class Office {
public string LocationName{set; get;}
public string Name{set; get;}
public int MaxCapacity{set; get;}
public IEnumerable<string> AvailableResources{set; get;}
}
public class Request {
public string PreferedNeigborHood{set; get;}
public int CapacityNeeded{set; get;}
public IEnumerable<string> ResourcesNeeded{set; get;}
}

相关问题链接

检查一个数组是否是另一个

的子集使用linq

判断一个序列是否包含另一个序列的所有元素

相关内容

  • 没有找到相关文章