问题描述
您好,
我有两个列表:
A = {ID,Name} = {(Null,John),( Null,Mary),(Null,Andrew),(Null,
Peter)}
B = {ID,Name} = {(1,John ),(2,Robert),(3,Angela),(4,Andrew)}
我想找到A中哪些项目不存在于B中:
1.将项目添加到B并使用函数GetID()中的ID。 B
将成为:
B = {ID,Name} = {(1,John),(2,Robert),(3,Angela) ,(4,
Andrew),(231,Mary),(45,Peter)}
2.然后更新A列表,或者创建一个新的(C),获得创建的
项目:
C = {ID,Name} = {(231,Mary),(45,彼得)}
如何使用LINQ执行此操作?
我一直在尝试加入,但使用的内容为not equals ;但
它不起作用。
我想我可能需要分三步完成:
1.在B中获取C中不存在的项目;
2.在C中插入这些项目;
3.从C再次获取这些项目以获取创建的ID这是
通讯员的名字。
有人可以帮我解决这个问题吗?
谢谢,
Miguel
Hello,
I have two Lists:
A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null,
Peter) }
B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) }
I want to find which items in A do not exist in B then:
1. Add the items to B and using an ID from the function GetID(). B
would become:
B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4,
Andrew), (231, Mary), (45, Peter) }
2. Then update the A list, or create a new one (C), to get the created
items:
C = {ID, Name} = { (231, Mary), (45, Peter) }
How can I do this with LINQ?
I have been trying a join but with the something as "not equals" but
it does not work.
I think I might need to do this in 3 steps:
1. Get items in B that do not exist in C;
2. Insert those items in C;
3. Get those items again from C to get the created ID''s with the
correspondent names.
Could someone help me out with this?
Thanks,
Miguel
推荐答案
在内存中,或者你真的想在SQL中这样做吗?内存中的一些
解决方案可能无法通过LINQ to SQL工作。
您可以使用Except运算符找出A中的哪些元素不是
B中的
,必要时传入IEqualityComparer中的项目。
(Marc:如果你正在读这个 - 我们应该写一个
ProjectionEqualityComparer和ProjectionComparer一样?
而不是ThenBy我们也可以用And组合
IEqualityComparers ...... )
Jon
In memory, or are you actually trying to do this in SQL? Some
solutions for in memory may not work via LINQ to SQL.
You can use the Except operator to find out which elements in A aren''t
in B, passing in an IEqualityComparer for the items if necessary.
(Marc: if you''re reading this - should we write a
ProjectionEqualityComparer along the same lines as ProjectionComparer?
Instead of "ThenBy" we could also have "And" to combine
IEqualityComparers...)
Jon
这篇关于Linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!