2015-11-09 117 views
1

我有两个列表,我想合并成一个列表。 Active Directory中的计算机列表以及SCCM中的计算机列表。C#合并两个对象列表

利斯塔包含:

public string ComputerName { get; set; } 
    public string OperatingSystem { get; set; } 
    public DateTime? LastLogon { get; set; } 

数组listB包含:

public string ComputerName { get; set; } 
    public DateTime[] AgentTime { get; set; } 
    public string LastLogonUserName { get; set; } 

我想通过计算机名进行合并,但如果我这样做与加盟使用(例如):

var query = from ObjectA in ListA 
      join ObjectB in ListB on ObjectA.ComputerName equals ObjectB.ComputerName 
      select new { computername = ObjectA.ComputerName, lastlogonusername = ObjectB.LastLogonUserName }; 

它只显示两个列表的结果。我要让计算机列表中AD与那些还含有SCCM

ListC附加信息:

public string ComputerName { get; set; } 
    public string OperatingSystem { get; set; } 
    public DateTime? LastLogon { get; set; } 
    public DateTime[] AgentTime { get; set; } 
    public string LastLogonUserName { get; set; } 

什么是这样做的最佳方式?

+1

你得到的输出是你的内部联接的结果。 –

+1

请参阅以下网页上的左外连接:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – jdweng

+0

您正在通过'new {}'创建动态对象。如果需要,创建一个自定义类。 –

回答

2

你需要一个外部联接:

var query = 
     from ObjectA in ListA 
     join ObjectB in ListB on ObjectA.ComputerName equals ObjectB.ComputerName into tmp 
     from ObjectB in tmp.DefaultIfEmpty() 
     select new { computername = ObjectA.ComputerName, lastlogonusername = ObjectB?.LastLogonUserName }; 

注上ObjectB使用?.:这是因为如果在ListB没有匹配的项目,ObjectB将是无效的。

(如果你不使用C#6,你可以做ObjectB != null ? ObjectB.LastLogonUserName : null代替)

+0

谢谢托马斯的快速回答。从obove的评论中,我发现了左外连接。但是,谢谢你指点我。不是匹配过程而不是ObjectB == null? “”:ObjectB.LastLogonUserName。谢谢 – user2931144

1

为什么不把所有内容添加到一个列表中,然后只需通过ComputerName获取一个不同的列表?

// Convert all B objects to A objects 
var objectBsAsAs = ListB.Select(x => new ObjectA() { ComputerName = x.ComputerName, LastLogonUserName = x.LastLogonUserName }); 
// Add all B objects to the list of A objects 
var allComputers = ListA.AddRange(objectBsAsAs); 
// Get a distinct list based on ComputerName 
var distinct = ListA.Distint(new ComputerNameComparer()); 

private class ComputerNameComparer : IEqualityComparer<ObjectA> 
{ 
    public bool Equals(ObjectA a, ObjectA b) 
    { 
     return a.ComputerName == b.ComputerName; 
    } 
} 
+0

也感谢你 – user2931144

+0

不客气。你可以通过给答案投票表示感谢! :) – timothyclifford

1

像这样的东西可能会帮助 -

 var l1 = new List<A> 
     { 
      new A 
      { 
       ComputerName = Dns.GetHostName(), 
       LastLogon = DateTime.Now, 
       OperatingSystem = "Windows" 
      } 
     }; 

     var l2 = new List<B> 
     { 
      new B 
      { 
       AgentTime = new DateTime[]{DateTime.Now}, 
       ComputerName = Dns.GetHostName(), 
       LastLogonUserName = "me" 
      } 
     }; 


     var o = from r in l2 
       join q in l1 on r.ComputerName equals q.ComputerName 
       into grp 
       from p in grp.DefaultIfEmpty() 
       select new C 
       { 
        AgentTime = r.AgentTime, 
        ComputerName = p.ComputerName, 
        LastLogon = p.LastLogon, 
        OperatingSystem = p.OperatingSystem, 
        LastLogonUserName = r.LastLogonUserName 
       }; 
+0

也谢谢你。 – user2931144