我将简要解释函数正在做什么/应该做什么:
函数查找哪个原型最适合所有原型 提供的设施。
public List<ArchetypesLokaal> GetArchetypeUitFaciliteiten(List<FaciliteitenLokaalFixed> faciliteiten)
{
var archetypes = new List<ArchetypesLokaal>();
var sortedArchetypes = new List<ArchetypesLokaal>();
//Iterate over all the archetypes
foreach(var archetype in db.ArchetypesLokaals)
{
//Get all the facilities that are present in archetype
var temp = GetFaciliteitenVoorArchetype(archetype);
//Create a new list to add all the facilities that are matched
var temp2 = new List<FaciliteitenLokaalFixed>();
//Iterate over the facilities provided as a parameter, these are the facilities that have to be present in the archetype
foreach(var faciliteit in faciliteiten)
{
//CHeck if facility is present in an archetype
var aanwezig = temp.FirstOrDefault(t => t.Naam.Equals(faciliteit.Naam));
if (aanwezig!=null)
{
//Add if it's present
temp2.Add(faciliteit);
}
}
var needed = faciliteiten.Count;
var present = temp2.Count;
//Compare how many facilities were met
if (present / needed >= .5)
{
archetypes.Add(archetype);
}
}
//Todo: sort archetypes by % facilities that were met
return archetypes;
}
所以现在我想根据当前/需要对列表进行排序,因此我可以先显示最佳结果。我也不确定我所做的是否是处理此问题的最有效方法,但我确信我的代码现在正常工作。如果您有任何建议,请务必使用。
谢谢。
答案 0 :(得分:1)
如果我理解正确,您需要根据present = temp2.Count
局部变量对输出进行排序。您可以将结果包装在某个容器中,例如Tuple
:
var archetypes = new List<Tuple<int, ArchetypesLokaal>>();
//Iterate over all the archetypes
foreach(var archetype in db.ArchetypesLokaals)
{
//Get all the facilities that are present in archetype
var temp = GetFaciliteitenVoorArchetype(archetype);
//Create a new list to add all the facilities that are matched
var temp2 = new List<FaciliteitenLokaalFixed>();
//Iterate over the facilities provided as a parameter, these are the facilities that have to be present in the archetype
foreach(var faciliteit in faciliteiten)
{
//CHeck if facility is present in an archetype
var aanwezig = temp.FirstOrDefault(t => t.Naam.Equals(faciliteit.Naam));
if (aanwezig!=null)
{
//Add if it's present
temp2.Add(faciliteit);
}
}
var needed = faciliteiten.Count;
var present = temp2.Count;
//Compare how many facilities were met
if (present / needed >= .5)
{
archetypes.Add(new Tuple<int, ArchetypesLokaal>(present, archetype));
}
}
return archetypes.OrderByDescending(q => q.Item1).Select(q => q.Item2).ToList();
答案 1 :(得分:1)
所以这不是一个完整的解决方案,但假设你在Archetype中有一个Facilteits的嵌套对象
var matchedArchetypes = archetypes
.Select(x => new { arch = x, cnt = x.Faciliteits.Join(faciliteitNaams, a => a.Naam, b => b.Naam, (a, b) => a).Count() })
.ToList();
将为您提供原型和匹配设施的数量
答案 2 :(得分:1)
正如其他人所述:我首先将其映射到原型/计数列表中,然后将其填入您需要的列表中(并根据此信息进行排序)。像这样:
var archetypeUitFaciliteiten = db.ArchetypesLokaals.Select(archetypeLokaal => {
var faciliteitenVoorArchetype = GetFaciliteitenVoorArchetype(archetypeLokaal);
return new {
ArcheType = archetypeLokaal,
Present = faciliteiten.Count(f => faciliteitenVoorArchetype.FirstOrDefault(t => t.Naam.Equals(f.Naam)) != null),
Needed = faciliteiten.Count
};
});
现在,您可以根据示例中的“当前/需要”比率将此列表(Select
)映射到列表中,或者根据这些属性对此列表进行排序(OrderBy
)。
(对不起,如果我屠杀了这种语言 - 我不会说荷兰语)