首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用linq在多个属性之间找到最佳匹配

使用linq在多个属性之间找到最佳匹配
EN

Stack Overflow用户
提问于 2015-06-29 11:46:50
回答 3查看 2.5K关注 0票数 2

我试图使用linq在一组属性的自定义对象列表中找到最佳匹配。在下面创建的MyObjects列表中,我希望在MyObject的所有四个属性中找到与testObject最接近的一个:

代码语言:javascript
运行
复制
IList<MyObject> list = new List<MyObject>();

list.Add(new MyObject { Property1 = "A", Property2 = "B", Property3 = "C", Property4 = "D" });
list.Add(new MyObject { Property1 = "A", Property2 = "A", Property3 = "C", Property4 = "D" });
list.Add(new MyObject { Property1 = "A", Property2 = "A", Property3 = "A", Property4 = "D" });


var testObject = new MyObject { Property1 = "A", Property2 = "A", Property3 = "A", Property4 = "A" };

在上面的示例中,我希望将最后一个对象匹配为3,或者将4个属性与testObject中的属性匹配。

通过这样做,我可以计算出匹配的属性有多少:

代码语言:javascript
运行
复制
 var matchCount = list.Max(x => (x.Property1 == testObject.Property1 ? 1 : 0) +
        (x.Property2 == testObject.Property2 ? 1 : 0) +
        (x.Property3 == testObject.Property3 ? 1 : 0) +
        (x.Property4 == testObject.Property4 ? 1 : 0));

但是,除了写出一个非常长的linq表达式,检查每个属性组合上的3个匹配项之外,我还想不出如何获取与其中三个属性匹配的实体。理想情况下,我想要一个适合于具有10个属性的对象的解决方案。

有没有人知道是否有一种被接受的方法来做到这一点?

编辑

我从最初的问题中又遗漏了一条信息.如果有多个匹配对象,那么我需要选择与该级别匹配的对象列表(也就是说,如果有一个对象在3个属性上匹配,那么我需要找到与3个属性匹配的所有对象)。

解决方案

根据树懒的答案,我已经能够得到我想要的使用这个。我很想看看有没有人对此有更清晰的答案.

代码语言:javascript
运行
复制
var grouping = list.GroupBy(x => (x.Property1 == testObject.Property1 ? 1 : 0) +
(x.Property2 == testObject.Property2 ? 1 : 0) +
(x.Property3 == testObject.Property3 ? 1 : 0) +
(x.Property4 == testObject.Property4 ? 1 : 0));

var maxCount = grouping.Max(x => x.Key);
var resultSet = grouping.FirstOrDefault(x => x.Key == maxCount).Select(g => g).ToList();
EN

Stack Overflow用户

回答已采纳

发布于 2015-06-29 13:57:03

你也可以试试这个

代码语言:javascript
运行
复制
    IList<MyObject> list = new List<MyObject>();

    list.Add(new MyObject { Property1 = "A", Property2 = "B", Property3 = "C", Property4 = "D" });
    list.Add(new MyObject { Property1 = "A", Property2 = "A", Property3 = "C", Property4 = "D" });
    list.Add(new MyObject { Property1 = "A", Property2 = "A", Property3 = "A", Property4 = "D" });


    var testObject = new MyObject { Property1 = "A", Property2 = "A", Property3 = "A", Property4 = "A" };

//list of objects with 3 or matches
    var sorted = list.Select(x => new
    {
        MatchCount = (x.Property1 == testObject.Property1 ? 1 : 0)
                    + (x.Property2 == testObject.Property2 ? 1 : 0)
                    + (x.Property3 == testObject.Property3 ? 1 : 0)
                    + (x.Property4 == testObject.Property4 ? 1 : 0),
        MyObj = x
    })
    .OrderBy( x => x.MatchCount)
    .Where( x => x.MatchCount >= 3 );

//gets the first object from the list
    var match = sorted.Any() ? sorted.OrderBy(x => x.MatchCount).FirstOrDefault().MyObj : null;
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31114892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档