-5

I have searched for a few examples and not found one that is similar to what I have and what I want to achieve.

I have 2 lists

class object1
{
   string obj1_name;
   int obj1_qty;
}
List<object1> listA

class object2
{
   string obj2_name;
   int obj2_qty;
}
List<object2> listB;

Now, using ListA as the primary list I want to see if ListB contains an object with the same name and if so, what is the quantity and hence, does the obj1_qty = obj2_qty each other? if not, there is a difference and I need to show it, most likely in a 3rd list which would be the difference, of the qty's if they exist. Note, ListA can be bigger or smaller than ListB

  • Show All List A (master list contains all objects)
  • Show the difference between those names/qty's that exist in both lists.

Gracias


It seems there are multiple ways to accomplish this 'do-later' task . My goal was to use Objective-C and Cocoa as a solution. Initially, I had worries about 'blocking' the main thread in some way. It turns out NSTimer is one answer to the question. I ended up using:

NSTimer *timer = [[NSTimer alloc] initWithFireDate:date
                                          interval:0.1
                                            target:self
                                          selector:@selector(startSomeMethod:)
                                          userInfo:info
                                           repeats:NO];

to 'fire' a task at a later time.

4

2 回答 2

3

像加入一样的声音对你有用:

var query = from a in listA
            join b in listB on a.obj1_name equals b.obj2_name
            where a.obj1_qty != b.obj2_qty
            select new {
                Name = a.obj1_name,
                QtyA = a.obj1_qty,
                QtyB = b.obj2_qty,
                Diff = a.obj1_qty - b.obj2_qty
                };
于 2013-08-27T14:49:41.323 回答
2

最简单的方法就是使用 Linq 的连接

var items = from l1 in listA
            join l2tmp1 in listB on l1.obj1_name equals l2.obj2_name into l2tmp2
            from l2 in l2tmp2.DefaultIfEmpty();
            select new { 
                 ItemA = l1, 
                 ItemB = l2, 
                 Name = l1.obj1_name, 
                 Difference = (l2 == null) ? 0 : l1.ob1_qty - l2.ob2_qty
            };

items现在将持有IEnumerable一个匿名类,该类持有对 ListA 中项目的引用、对 listB 中项目的引用、匹配的名称和差异。

于 2013-08-27T14:41:50.440 回答