C#合并两个对象列表

时间:2015-11-09 09:14:18

标签: c# linq object

我有两个列表,我想合并到一个列表中。 Active Directory中的计算机列表和SCCM中的计算机列表。

ListA包含:

    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; }

我想通过ComputerName合并,但如果我使用(示例)加入<:p>

    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; }

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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
                };