如何基于属性的部分信息比较两个对象列表? [英] How to compare two lists of objects based on partial information of property?

查看:66
本文介绍了如何基于属性的部分信息比较两个对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要基于DateTime属性的截断(我认为这是单词)版本比较两个对象列表:

Lets say you want to compare two lists of objects based on a truncated (I think this is the word) version of a DateTime property:

public class MyObject 
{
    public DateTime SomeDate {get;set;}
}

正常比较列表:

bool didItWork = myFirstList.Intersect(MySecondList).Any();

这里一切正常,除了我需要根据上面的DateTime属性执行此检查:

All fine and good here except I need to do this check based on the above DateTime property:

bool didItWork = myFirstLIst.Intersect(MySecondList).Any(someIntuitivePredicate);

如您所见,我不知道直观谓词是什么.

As you can see I don't know what the intuitive predicate is.

对于踢球者,我想在进行比较时将时间"部分从SomeDate中删除:

And for a kicker I want to cut the Time section off of SomeDate when I do the compare:

dateWithoutTime = myObject.SomeDate.Date;

时间并不只是实际日期重要.

The time is not important just the actual date.

推荐答案

看到您未标记答案,我将在这方面做个破解,但是答案确实是很好的努力,但这是您模糊的问题定义的结果.甚至您的注释如果一个列表中的任何一个在另一个列表中都不存在",也有些措辞尴尬.不是要烧烤您,而是要牢记这一点.在处理集合操作时,您必须退后一步,并认真仔细考虑要完成的工作.我将以此来解释您的目标,如果这是不正确的,则应使用更明确定义的问题定义来更新您的问题:

Seeing you unmarked the answer, I'm going to take a crack at this, however the answers really are good efforts, but a result of your vague problem definition. Even your comment "If there are any items in either list that do not exist in the other" is a little awkwardly worded. Not trying to grill you, but just keep that in mind. When dealing with set operations you have to take a step back and really think carefully about what you want to accomplish. I'm going to interpret your goal as this, and if this is incorrect, you should update your question with a more clearly defined problem definition:

我要检查并确保对于myFirstList中的每个项目, 在mySecondList中有一个具有相同SomeDate.Date的项目,反之 反之亦然.

I want to check and make sure that for every item in myFirstList, there is an item in mySecondList with the same SomeDate.Date, and vice versa.

如果您要从某个日期开始截断时间,那么我推测可能存在两个项目具有相同日期的情况.如果不是这种情况,那么可以使用一些更简单的方法,例如加入或相交列表,以及检查结果与原始计数是否相等(验证所有项目均找到匹配项).其他使用join或intersect的答案效果不佳,因为它们使用.Any(),如果只有一项匹配而不是全部匹配,则返回true.

If you are truncating time from a date, then I would speculate there might be cases where there are two items with the same date. If this were not the case, there are easier methods that take approaches such as joining or intersecting the lists and checking that the results have equal Count as the originals(which verifies that all items found a match). The other answers using join or intersect don't quite work, because they use .Any() which will return true if just one of the items matches, rather than all.

.All将确保一个列表中的所有项目均符合某些条件.在这种情况下,请确保第二个列表中的每个项目都匹配.

.All will make sure that all items in one list meet some criteria. In this case, make sure every item has a match in the second list.

bool isListEqual = myFirstList.All(x=> 
      mySecondList.Select(y=>y.SomeDate.Date).Contains( x.SomeDate.Date) )

isListEqual = isListEqual && mySecondList.All(x=> 
      myFirstList.Select(y=>y.SomeDate.Date).Contains( x.SomeDate.Date) )

我们双向执行,以确保第二个列表中没有项目,而第一个列表中没有匹配的项目.

We do it both directions to ensure there are no items in the second list without a matching item in the first.

如果我们知道没有重复项,那么另一种方法是加入列表并计算结果以确保它们与原始结果匹配.或者,我们可以简单地消除重复项以生成不同的列表.您还可以使用哈希集的SetEquals或IEnumerable的SequenceEquals.如果您知道结果已经按特定顺序排列,则SequenceEquals会更好.

If we knew there were no duplicates, another approach would be to join the lists and count the results to ensure they match the originals. Or we could simply eliminate duplicates to generate distinct lists. You can also use SetEquals of hashset, or SequenceEquals of IEnumerable. SequenceEquals is better if you know your results are already in a particular order.

   var firstDates = myFirstList.Select(l=>l.SomeDate.Date).Distinct().OrderBy(d=>d).ToList();
   var secondDates = mySecondList.Select(l=>l.SomeDate.Date).Distinct().OrderBy(d=>d).ToList();

   bool isListEqual = firstDates.SequenceEqual(secondDates);

我认为您应该从中获取的最重要的信息是,该方法取决于您可以对输入数据进行何种假设.

I think the most important thing you should take from this, is the approach is dependent on what assumptions you can make about your input data.

这个问题涵盖了几种方法,并且其重复项也与之链接:

This question covers several approaches, and the duplicate it is linked to as well:

检查两个列表是否相等

这篇关于如何基于属性的部分信息比较两个对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆