在列表 C# 中查找最佳匹配实例的最佳实践

Best Practice to find best matching instance in a List C#

对于你们大多数人来说,这当然是一个非常简单的问题。 但我目前正在努力寻找解决方案。

假设您有一个猫列表 (List),其中每只猫都有一个婴儿列表 (Kitten)

public class Cat
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Race { get; set; }
        public bool Gender { get; set; }
        public List<Kitten> Babys { get; set; }  
    }

public class Kitten
    {
        public string Name { get; set; }
        public double Age { get; set; }
        public bool Gender { get; set; }
    }

现在我想找到最符合给定要求的猫。一只猫很可能只满足 3 个要求中的 2 个。我只是想找到最符合我要求的猫。

我的要求可能是:

我的实际解决方案是比较所有属性并选择具有最多匹配属性的属性。但这不是通用的,我相信有很多更好的方法来做到这一点。

提前致谢

如果您真的要比较 2 或 3 个要求,您可以通过以下方式简化使用 Linq 的过程:

// try to find with 3 requirements
var foundCats = catList.Where(t => t.Name == desiredName && 
                                   t.Age == desiredAge &&
                                   t.Babys.Any(k => k.Name == desiredKitten)
                             ).ToList();

if (foundCats.Any())
{
    // you found the desired cat (or cats)
    return foundCats;
}

// try to find with 2 requirements
foundCats = catList.Where(t => 
    (t.Name == desiredName && t.Age == desiredAge) ||
    (t.Name == desiredName && t.Babys.Any(k => k.Name == desiredKitten)) ||
    (t.Age == desiredAge && t.Babys.Any(k => k.Name == desiredKitten)
).ToList();

if (foundCats.Any())
{
    // you found the desired cat (or cats)
    return foundCats;
}

// try to find with only 1 requirement
foundCats = catList.Where(t => t.Name == desiredName || 
                               t.Age == desiredAge ||
                               t.Babys.Any(k => k.Name == desiredKitten)
                         ).ToList();
return foundCats;

好吧,我没有机会测试这个解决方案,但你可以试试这个:

假设您有一个猫的列表:

var cats = new List<Cat>();

现在您已经定义了您的标准:

var desiredName = "Micky";
var desiredAge = 42;
var desiredKitten = "Mini";

然后你必须得到你想要的猫:

var desiredCat = cats
        .Select(c => new {
            Rating = 
                Convert.ToInt32(c.Age == desiredAge) +       // Here you check first criteria
                Convert.ToInt32(c.Name == desiredName) +     // Check second
                Convert.ToInt32(c.Babys.Count(b => b.Name == desiredKitten) > 0),   // And the third one
            c })
        .OrderByDescending(obj => obj.Rating) // Here you order them by number of matching criteria
        .Select(obj => obj.c) // Then you select only cats from your custom object
        .First(); // And get the first of them

请检查这是否适合您。 如果您需要更具体的答案或一些编辑,我可以添加。

所以,我看到问题是你不知道在不久的将来你是否会有更多的财产,所以我建议你去努力反思,以下是丑陋的 af 但你可能可以(你会)让它变得更好,希望能很好地为你服务:

public static List<Cat> CheckProperties(List<Cat> inCatList, Cat inQueryCat)
{
    Dictionary<Cat, List<PropertyInfo>> dict = new Dictionary<Cat, List<PropertyInfo>>();

    foreach (PropertyInfo pI in inQueryCat.GetType().GetProperties())
    {
        var value = pI.GetValue(inQueryCat);

        if (value != null)
        {
            var cats = inCatList.Where(cat => cat.GetType().GetProperty(pI.Name).GetValue(cat).Equals(value));

            foreach (Cat cat in cats)
            {
                if (dict.ContainsKey(cat))
                {
                    dict[cat].Add(pI);
                }
                else
                {
                    dict.Add(cat, new List<PropertyInfo>() {pI});
                }
            }
        }
    }

    int max = Int32.MinValue;
    foreach (KeyValuePair<Cat, List<PropertyInfo>> keyValuePair in dict)
    {
        if (keyValuePair.Value.Count > max)
        {
            max = keyValuePair.Value.Count;
        }
    }

    return dict.Where(pair => pair.Value.Count == max).Select(pair => pair.Key).ToList();
}

虽然这是最通用的解决方案(需要一些边缘案例改进):

  public class ReflectCmpare
    {
        public PropertyInfo PropertyInfo { get; set; }
        public dynamic Value { get; set; }
    }


    public Cat GetBestCat(List<Cat> listOfCats, List<ReflectCmpare> catParamsToCompare, List<ReflectCmpare> kittensParamsToCompare)
    {
        var bestScore = 0;
        var ret = listOfCats[0];
        foreach (var cat in listOfCats)
        {
            var score = catParamsToCompare.Sum(param => param.PropertyInfo.GetValue(cat, null) == param.Value ? 1 : 0);
            foreach (var baby in cat.Babys)
            {
                score+= kittensParamsToCompare.Sum(param => param.PropertyInfo.GetValue(baby, null) == param.Value ? 1 : 0);
            }

            if (score <= bestScore) continue;
            bestScore = score;
            ret = cat;
        }
        return ret;
    }

你真的应该考虑只做简单的比较功能 考虑到这个对象不是动态的,这是要走的路:

public Cat GetBestCat(List<Cat> listOfCats, string name , int? age , bool? gender, string race ,string babyName,int? babyAge,bool? babyGender )
    {
        var ret = listOfCats[0];
        var highestScore = 0;
        foreach (var cat in listOfCats)
        {
            var score = 0;
            score += name != null && cat.Name.Equals(name) ? 1 : 0;
            score += age.HasValue && cat.Age.Equals(age.Value) ? 1 : 0;
            score += gender.HasValue && cat.Gender.Equals(gender.Value) ? 1 : 0;
            score += race != null && cat.Race.Equals(race) ? 1 : 0;
            score += name != null && cat.Name.Equals(name) ? 1 : 0;
            score += cat.Babys
                .Where(k => babyName==null || k.Name.Equals(babyName))
                .Where(k => !babyAge.HasValue || k.Age.Equals(babyAge.Value))
                .Any(k => !babyGender.HasValue || k.Gender.Equals(babyGender.Value))?1:0;
            if (score <= highestScore) continue;
            highestScore = score;
            ret = cat;
        }

        return ret;
    }