gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:06:37 27 4
gpt4 key购买 nike

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

  • 参数:需要的设施列表
  • 返回:满足超过 50% 的设施的原型(prototype)列表需要
  • 原型(prototype)具有 X 数量的设施(存储在数据库中)。
  • 用户提供他想要满足的设施列表(参数)
  • 函数查找哪种原型(prototype)最适合所有提供的设施。

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

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

谢谢。

最佳答案

如果我理解正确,您需要根据 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();

关于C# 使用外部条件对列表进行排序的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40991879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com