C#使用外部标准对列表进行排序的方法

时间:2016-12-06 09:28:10

标签: c#

我将简要解释函数正在做什么/应该做什么:

  • 参数:所需设施清单
  • 返回:满足50%以上设施的原型列表 是必需的
  • 原型具有X个设施(存储在DB中)。
  • 用户提供他想要的设施清单(参数)
  • 函数查找哪个原型最适合所有原型 提供的设施。

    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;
    }
    

所以现在我想根据当前/需要对列表进行排序,因此我可以先显示最佳结果。我也不确定我所做的是否是处理此问题的最有效方法,但我确信我的代码现在正常工作。如果您有任何建议,请务必使用。

谢谢。

3 个答案:

答案 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)。

(对不起,如果我屠杀了这种语言 - 我不会说荷兰语)