按条件在列表中获取元素与其他列表

时间:2022-01-14 18:14:46

I have two lists of the same object, and they are of different sizes.

我有两个相同对象的列表,它们的大小不同。

I wan't all in objects List1 where tmDate does not exist in List2.

我不想在List2中的所有对象中,其中tmDate不存在于List2中。

public class MyObj
{
    public MyObj(DateTime tmDate)
    {
        this.tmDate = tmDate;
    }
    public DateTime tmDate { get; set; }
}

var List1 = new List<MyObj>();
        var List2 = new List<MyObj>();

        List1.Add(new MyObj(new DateTime(2015, 01, 01)));
        List1.Add(new MyObj(new DateTime(2015, 01, 02)));
        List1.Add(new MyObj(new DateTime(2015, 01, 03)));

        List2.Add(new MyObj(new DateTime(2015, 01, 01)));
        List2.Add(new MyObj(new DateTime(2015, 01, 02)));

        var notMatchingObj =
            List1.Where(
                l1 => List2.Any(l2 => l2.tmDate != l1.tmDate));

This code gives me wrong output..I only want MyObj with tmDate 2015-01-03 in this case

这段代码给了我错误的输出..我只想在这种情况下使用tmDate 2015-01-03 MyObj

2 个解决方案

#1


You could try this one:

你可以尝试这个:

var result = list1.Where(item=>!list2.Any(item2=>item2.tmDate==item.tmDate));

This list2.Any(item2=>item2.tmDate==item.tmDate) returns true if there is any item in the list2 with the same date with the item. Otherwise it returns false. Taking it's negation, we get that we want.

如果list2中的任何项目与项目具有相同的日期,则此list2.Any(item2 => item2.tmDate == item.tmDate)将返回true。否则返回false。把它当作否定,我们得到了我们想要的。

On the other hand this List2.Any(l2 => l2.tmDate != l1.tmDate) returns true if any item found in List2, whose date is not the same with the date of l1. This doesn't mean that an object with the same date with the l1 doesn't exist. The above will return true if the first item you check has date different with l1. However an item in List2 may exist with the same date with l1.

另一方面,如果在List2中找到任何项目,则List2.Any(l2 => l2.tmDate!= l1.tmDate)返回true,其日期与l1的日期不同。这并不意味着不存在与l1具有相同日期的对象。如果您检查的第一个项目的日期与l1不同,则上述内容将返回true。但是,List2中的项目可能与l1具有相同的日期。

#2


Try

var notMatchingObj =
                from list1 in List1
                where (!List2.Any(l2 => l2.tmDate == list1.tmDate))
                select new { list1.tmDate };

#1


You could try this one:

你可以尝试这个:

var result = list1.Where(item=>!list2.Any(item2=>item2.tmDate==item.tmDate));

This list2.Any(item2=>item2.tmDate==item.tmDate) returns true if there is any item in the list2 with the same date with the item. Otherwise it returns false. Taking it's negation, we get that we want.

如果list2中的任何项目与项目具有相同的日期,则此list2.Any(item2 => item2.tmDate == item.tmDate)将返回true。否则返回false。把它当作否定,我们得到了我们想要的。

On the other hand this List2.Any(l2 => l2.tmDate != l1.tmDate) returns true if any item found in List2, whose date is not the same with the date of l1. This doesn't mean that an object with the same date with the l1 doesn't exist. The above will return true if the first item you check has date different with l1. However an item in List2 may exist with the same date with l1.

另一方面,如果在List2中找到任何项目,则List2.Any(l2 => l2.tmDate!= l1.tmDate)返回true,其日期与l1的日期不同。这并不意味着不存在与l1具有相同日期的对象。如果您检查的第一个项目的日期与l1不同,则上述内容将返回true。但是,List2中的项目可能与l1具有相同的日期。

#2


Try

var notMatchingObj =
                from list1 in List1
                where (!List2.Any(l2 => l2.tmDate == list1.tmDate))
                select new { list1.tmDate };