Linq查询以获取每个IP的公司数量

时间:2015-10-14 15:09:31

标签: c# linq

我要两个清单。

        var companies = new List <CompanyData> {
            new Data {Company= "company1", Location= "location1"},
            new Data {Company= "company2", Location= "location1"},
            new Data {Company= "company1", Location= "location2"},
            new Data {Company= "company3", Location= "location1"},
            new Data {Company= "company2", Location= "location2"},
            new Data {Company= "company1", Location= "location3"},
            new Data {Company= "company1", Location= "location4"}
        };

        var locations= new List<LocationData> {
            new Data {IP= "ip1", Location= "location1"},
            new Data {IP= "ip1", Location= "location2"},
            new Data {IP= "ip2", Location= "location3"},
            new Data {IP= "ip3", Location= "location4"},
        };

我想列出每个IP位置的公司名单。如何通过LINQ?

期待结果

var result = new SortedList<string,List<string>>()
                  {"ip1", new List<string>{"company1","company2",..}}

提前致谢。

2 个答案:

答案 0 :(得分:0)

您应该能够根据自己的需要进行调整:

ZlibStream

输出:

class Program
{
    static void Main(string[] args)
    {
        var companies = new List<Company> 
                            {new Company {Name = "company1", Location = "location1"},
                             new Company {Name = "company2", Location = "location1"},
                             new Company {Name = "company1", Location = "location2"},
                             new Company {Name = "company3", Location = "location1"},
                             new Company {Name = "company2", Location = "location2"},
                             new Company {Name = "company1", Location = "location3"},
                             new Company {Name = "company1", Location = "location4"}};

        var locations = new List<IP>
                            {new IP {Name = "ip1", Location = "location1"},
                             new IP {Name = "ip1", Location = "location2"},
                             new IP {Name = "ip2", Location = "location3"},
                             new IP {Name = "ip3", Location = "location4"}};

        var query = locations.Join(companies, 
                                   ip => ip.Location, 
                                   c => c.Location, 
                                   (ip, c) => new JoinResult() { IP = ip, 
                                                                 Company = c });

        foreach (var result in query)
        {
            Console.WriteLine("IP: {0} - {1}, Company: {2}", 
                              result.IP.Name, 
                              result.IP.Location, 
                              result.Company.Name);
        }

        Console.ReadKey();
    }

    public class Company
    {
        public string Name;
        public string Location;
    }

    public class IP
    {
        public string Name;
        public string Location;
    }

    public class JoinResult
    {
        public Company Company;
        public IP IP;
    }
}

答案 1 :(得分:0)

继上述测试的上述解决方案后,您可以使用LINQ中的Group按IP对公司进行分组:

void Main()
{
    var companies = new List<Company>
                            {new Company {Name = "company1", Location = "location1"},
                             new Company {Name = "company2", Location = "location1"},
                             new Company {Name = "company1", Location = "location2"},
                             new Company {Name = "company3", Location = "location1"},
                             new Company {Name = "company2", Location = "location2"},
                             new Company {Name = "company1", Location = "location3"},
                             new Company {Name = "company1", Location = "location4"}};

    var locations = new List<IP>
                            {new IP {Name = "ip1", Location = "location1"},
                             new IP {Name = "ip1", Location = "location2"},
                             new IP {Name = "ip2", Location = "location3"},
                             new IP {Name = "ip3", Location = "location4"}};

    var query = locations.Join(companies,
                               ip => ip.Location,
                               c => c.Location,
                               (ip, c) => new JoinResult()
                               {
                                   IP = ip,
                                   Company = c
                               });

    var companiesByIp = query.GroupBy(b => b.IP.Name);

    foreach (var ip in companiesByIp)
    {
        Console.WriteLine(string.Format("IP: {0}, {1} companies", ip.Key, ip.Count()));
    }

}

public class Company
{
    public string Name;
    public string Location;
}

public class IP
{
    public string Name;
    public string Location;
}

public class JoinResult
{
    public Company Company;
    public IP IP;
}

这将输出以下内容:

IP: ip1, 5 companies
IP: ip2, 1 companies
IP: ip3, 1 companies