简体   繁体   English

C#LINQ加入两个列表

[英]c# LINQ joining two lists

I'm fighting while trying to join two lists together with LINQ. 我正在努力与LINQ一起加入两个列表。

What I got... 我得到了...

Class Person: 班级人员:

class Person
{
    public string Name { get; private set; }
    public int Age { get; private set; }
    public bool Parent { get; private set; }
    public bool Child { get; private set; }
    public int Housenumber { get; private set; }

    public Person(string name, int age, bool parent, bool child, int housenumber)
    {
        Name = name;
        Age = age;
        Parent = parent;
        Child = child;
        Housenumber = housenumber;
    }
}

class House: 类房:

class House
{
    public int Housenumber { get; private set; }
    public int Rooms { get; private set; }
    public string Color { get; private set; }

    public House(int housenumber, int rooms, string color)
    {
        Housenumber = housenumber;
        Color = color;
        Rooms = rooms;
    }
}

List of people: 人数清单:

private static List<Person> people = new List<Person>
    {
        new Person("Joel", 12, false, true, 1),
        new Person("jana", 22, false, false, 2),
        new Person("Housi", 45, true, false, 3),
        new Person("Kurt", 25, false, false, 4),
        new Person("Sebastian", 65, true, false, 1),
        new Person("George", 14, false, true, 2),
        new Person("Noel", 50, true, false, 3)
    };

And a list of houses: 以及房屋清单:

private static List<House> houses = new List<House>
    {
        new House(1, 4, "blue"),
        new House(2, 2, "red"),
        new House(3, 3, "black"),
        new House(4, 1, "violett")
    };

What I would like to to is to create a new list containing 'House' objects. 我要创建一个包含“ House”对象的新列表。 But only those houses where at least two people live in! 但是,只有那些至少有两个人居住的房屋!

I'm stuck here: 我被困在这里:

var houseWithMorePeople = from house in houses
            join person in people
                on house.Housenumber equals person.Housenumber
            join person2 in people
                on person.Housenumber equals person2.Housenumber
            select house;

You can simply use Where and Count like this: 您可以像这样简单地使用WhereCount

var result = houses
    .Where(h => people.Count(p => p.Housenumber == h.Housenumber) >= 2)
    .ToList();

There is no need for a join . 不需要join You can do it with Where and Count : 您可以使用WhereCount

var houseWithMorePeople = houses.Where(house => 
    persons.Count(person => person.Housenumber == house.Housenumber) > 1);

I'm not sure how to best convert Count() into query language, so this is my best guess: 我不确定如何最好地将Count()转换为查询语言,所以这是我的最佳猜测:

var houseWithMorePeople = 
       from house in houses
       where (
               from person in persons 
               where person.Housenumber == house.Housenumber 
               select person
       ).Count() > 1 
       select house;

I prefer the method syntax. 我更喜欢方法语法。

In query syntax with GroupBy : 在使用GroupBy查询语法中:

IEnumerable<House> houseWithMorePeople = 
        from house in houses
        join person in people
            on house.Housenumber equals person.Housenumber
        group house by house.Housenumber into multiResidentsHouse
        where multiResidentsHouse.Count() >= 2
        select multiResidentsHouse.First();

If you want both informations, the house and it's residents: 如果您同时需要这两个信息,则房屋及其居民:

var houseIncludingResidents =
    from house in houses
    join person in people
        on house.Housenumber equals person.Housenumber
    let resident = new { house, person }
    group resident by house.Housenumber into multiResidentsHouse
    where multiResidentsHouse.Count() >= 2
    select new 
    {
        House = multiResidentsHouse.First().house,
        Residents = multiResidentsHouse.Select(x => x.person).ToList()
    };

I'm really surprised by the answers saying "there is no need of join, simply do blah-blah". 我的回答让我感到非常惊讶,说“不需要加入,只需做等等。” After all, what is more important - do it in a short way or do it in a right (and more performant) way? 毕竟,更重要的是-以简短的方式还是以正确的(以及表现更好的方式)进行?

You are on the right track. 您走在正确的轨道上。 Just instead of a regular join you need a group join , which as stated in the Enumerable.GroupJoin documentation: 正如Enumerable.GroupJoin文档中所述,除了常规join还需要一个 联接

Correlates the elements of two sequences based on key equality, and groups the results. 根据键相等性对两个序列的元素进行关联,并对结果进行分组。

Once the data is correlated, you can perform efficient checks/aggregates on the related data set, like counting as in your case. 数据关联后,您可以对相关数据集执行有效的检查/汇总,例如根据您的情况进行计数。

var houseWithMorePeople = 
    from house in houses
    join person in people 
        on house.Housenumber equals person.Housenumber
        into peopleInAHouse
    where peopleInAHouse.Count() >= 2
    select house;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM