简体   繁体   English

c# 如果两个属性相同,则将两个类合并到一个列表中

[英]c# combine two classes in one list if two properties are the same

I practice OOP and have this question.我练习 OOP 并有这个问题。 Let's say I have three classes:假设我有三个课程:

public class Car  {
int id { get; set; }
string name { get; set; }
DateTime dateOfProduction { get; set; }
...
}

and

public class Customer {
int customerId { get; set; }
int carId { get; set; }
DateTime pickupDate { get; set; }
...
}

and

public class Fabric {
int id { get; set; }
int Fabricname { get; set; }
int CarId { get; set; }
int numberOfEmployees { get; set; }
string fabricLocation { get; set; }
... }

Now I want one List with the all other information from Fabric(Fabricname, numberOfEmpoyees) and all attributes of Customer (name, id, ...) if they have the same CarId现在我想要一个列表,其中包含 Fabric(Fabricname, numberOfEmpoyees) 中的所有其他信息以及 Customer 的所有属性(名称、id、...),如果它们具有相同的 CarId

-->CarId 12:
CustomerId: 294
CustomerName: Mike
FabricLocation: Munich
NumberOfEmpoyees: 100
PickupDate: 24/04/2020

--> CarId 13:
...

How do I do that / what is the best way?我该怎么做/最好的方法是什么? In the end I want to display all data in a (html) table in blazor.最后,我想在 blazor 的(html)表中显示所有数据。

Would be very happy if someone could help me如果有人可以帮助我会很高兴

you should use .GroupJoin in linq你应该在 linq 中使用.GroupJoin

var result = fabricsList.GroupJoin(customersList,f=>f.CarId,cu=>cu.CarId,(f,cu)=>new {FabricName = f.FabricName,pickUpDate= cu.pickUpDate})

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

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