我在尝试与LINQ一起加入两个列表时正在努力。
我得到了什么...
班主任:
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
{
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;
}
}
人员名单:
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)
};
以及房屋清单:
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")
};
我想要的是创建一个包含'House'对象的新列表。 但只有那些至少有两个人居住的房子!
我被困在这里:
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;
答案 0 :(得分:4)
您可以像这样简单地使用Where
和Count
:
var result = houses
.Where(h => people.Count(p => p.Housenumber == h.Housenumber) >= 2)
.ToList();
答案 1 :(得分:3)
不需要join
。您可以使用Where
和Count
:
var houseWithMorePeople = houses.Where(house =>
persons.Count(person => person.Housenumber == house.Housenumber) > 1);
我不确定如何最好地将Count()
转换为查询语言,所以这是我最好的猜测:
var houseWithMorePeople =
from house in houses
where (
from person in persons
where person.Housenumber == house.Housenumber
select person
).Count() > 1
select house;
我更喜欢方法语法。
答案 2 :(得分:2)
使用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();
如果你想要两种信息,房子和它的居民:
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()
};
答案 3 :(得分:2)
我真的很惊讶于答案说“没有必要加入,只是做等等”。毕竟,更重要的是 - 以简短的方式做到这一点,还是以正确的(更高效的)方式做到这一点?
你走在正确的轨道上。只需代替常规join
,您需要group join,如Enumerable.GroupJoin文档中所述:
根据键相等性将两个序列的元素相关联,并对结果进行分组。
一旦数据相关,您就可以对相关数据集执行有效的检查/聚合,就像在您的情况下进行计数一样。
var houseWithMorePeople =
from house in houses
join person in people
on house.Housenumber equals person.Housenumber
into peopleInAHouse
where peopleInAHouse.Count() >= 2
select house;