c# - Get elements in list by condition against other list -
i have 2 lists of same object, , of different sizes.
i wan't in objects list1 tmdate not exist in 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 want myobj tmdate 2015-01-03 in case
you try 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 item in list2
same date item
. otherwise returns false. taking it's negation, want.
on other hand list2.any(l2 => l2.tmdate != l1.tmdate)
returns true if item found in list2
, date not same date of l1
. doesn't mean object same date l1
doesn't exist. above return true if first item check has date different l1
. item in list2
may exist same date l1
.
Comments
Post a Comment