简体   繁体   English

连接两个对象列表:一个列表中的所有属性,另一个列表中的某些属性

[英]Join two lists of objects: All properties from one list, only certain properties from the other one

I have two classes:我有两个班级:

class Class1
{
    public int ProductId  { get; set; }
    public string ProductName  { get; set; }
    public int Amount  { get; set; }
    public string Category { get; set; }
}

class Class2
{
    public id ProductId  { get; set; }
    public DateTime UpdateTime { get; set; }
    public int ItemState  { get; set; }
    public decimal Price { get; set; }
    public string ImageUrl { get; set; }
}

I create two lists for each of this class:我为每个班级创建了两个列表:

public List<Class1> listClass1 { get; set; } = new List<Class1>();
public List<Class2> listClass2 { get; set; } = new List<Class2>();

Now I want to create a new list by joining both lists on the ProductID containing all properties from Class1 and just UpdateTime and ItemState from Class2 .现在我想通过在连接两个列表创建一个新的列表ProductID包含来自所有属性Class1 ,只是UpdateTimeItemStateClass2 What is the best way to do this?做这个的最好方式是什么?

(The ultimate goal is to let the user save the current session to let him continue with his work later, for this I need the complete listClass1 and the mentioned properties of listClass2 . The idea is to use an XmlSerializer on the merged list and save it into an XML file. To open the file, I want to deserialize it. If you have a better approach to let the user save his session, I'm all ears.) (最终目标是让用户保存当前会话以让他以后继续他的工作,为此我需要完整的listClass1和提到的listClass2属性。想法是在合并列表上使用XmlSerializer并保存它到一个 XML 文件中。要打开该文件,我想对其进行反序列化。如果您有更好的方法让用户保存他的会话,我会全力以赴。)

I would suggest to make a third class which represents the joined parameter set.我建议创建一个代表连接参数集的第三个类。

public class Class1_2
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public int Amount { get; set; }
    public string Category { get; set; }

    public DateTime UpdateTime { get; set; }
    public int ItemState { get; set; }
}

How to join 2 Lists is described in LINQ Join 2 Lists . LINQ Join 2 Lists 中描述了如何 加入 2 个列表 So you would end up with something like this:所以你最终会得到这样的结果:

List<Class1_2> joinedList = (from item1 in listClass1
                             join item2 in listClass2
                             on item1.ProductId equals item2.ProductId
                             select new Class1_2
                             {
                                 ProductId = item1.ProductId,
                                 Amount = item1.Amount,
                                 ProductName = item1.ProductName,
                                 Category = item1.Category,
                                 ItemState = item2.ItemState,
                                 UpdateTime = item2.UpdateTime
                             }).ToList();

Explanation: You join the items on the particular property.说明:您加入特定属性上的项目。 If it is equal in both lists you take the item and construct a new one with the selected properties.如果它在两个列表中相等,则获取该项目并使用所选属性构建一个新项目。 The join would also work without Class1_2 but then you get a result of dynamic type.连接也可以在没有Class1_2情况下工作,但是您会得到dynamic类型的结果。 The advantage of the new Class_1_2 is that you can use it for serialization with a defined structure.新的Class_1_2的优点是您可以将其用于具有定义结构的序列化。

EDIT编辑

you could also use a second constructor in Class1_2您还可以在Class1_2使用第二个构造Class1_2

public Class1_2(Class1 c1, Class2 c2)
{
    ProductId = c1.ProductId;
    Amount = c1.Amount;
    ProductName = c1.ProductName;
    Category = c1.Category;
    ItemState = c2.ItemState;
    UpdateTime = c2.UpdateTime;
}

that would simplify a little the select statement to:这将把select语句简化为:

select new Class1_2(item1, item2)).ToList();

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

相关问题 C#列表:如何将元素从一个列表复制到另一个列表,但仅复制某些属性 - C# Lists: How to copy elements from one list to another, but only certain properties 如何在具有两个属性的对象列表中检查具有相同属性的所有对象是否也具有相同的其他属性? - How to check if in a list of Objects with two properties, all Objects with one equal property also have the same other property? 从对象列表中获取一个对象的属性 - Get properties of one object from the list of objects 根据属性将列表对象从一个列表复制到另一个列表 - Copy list objects from one list to another list based on properties 从对象列表中获取所有对象,其中对象属性之一在C#中为null或为空 - Get all objects from a list of objects where one of the object properties is null or empty in c# AutoMapper将一个列表映射到具有派生属性的两个大小相等的列表 - AutoMapper map one list to two equally sized lists with derived properties 将两个列表合并为一个列表 - Join two lists into one list 一次在linq中加入两个列表初始化属性 - join two list in linq initializing properties one time 合并两个列表中的属性 - merge properties from two lists 根据某些属性从数据库对象构建列表 - Build list from database objects based on certain properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM