简体   繁体   English

如何有条件地使用Linq将属性从一个列表复制到另一个列表

[英]How to copy properties from one list to another using Linq conditionally

I have two lists of custom objects. 我有两个自定义对象列表。
Both have DivisionId as common propery. 两者都将DivisionId作为通用属性。 I want to copy two parameters, PlanType and CoverageType , from List<Divisions> to List<Members> using Linq, whenever Member.DivisionId = Divisions.DivisionId . 每当Member.DivisionId = Divisions.DivisionId时,我想使用Linq从List<Divisions>复制两个参数PlanTypeCoverageTypeList<Members>

The Member and Divisions objects are given below : 成员和部门对象如下:

public class Members
{
  public string FirstName;
  public string LastName;
  public string DivisionId;
}

Public Class Divisions
{
  public string PlanType;
  public string CoverageType,
  public string DivisionId;
}

Thanks 谢谢

What you describe is Join 您描述的是加入

    var query = from m in members
                join d in divisions on m.DivisionId equals d.DivisionId
                select new { m.FirstName,
                            m.LastName,
                            m.DivisionId,
                            d.CoverageType,
                            d.PlanType
                        };

If you want to copy the item from index 0 in the first list to index 0 in the second, and so on for all of the other indexes you can do this: 如果要将项目从第一个列表的索引0复制到第二个列表的索引0,以此类推,对于所有其他索引,可以执行以下操作:

var pairs = members.Zip(divisions, (a, b) => new
{
    Member = a,
    Division = b,
});

foreach (var pair in pairs)
{
    Copy(pair.Member, pair.Division);
}

If the indexes don't match you need to do a join: 如果索引不匹配,则需要进行联接:

var pairs = members.Join(divisions
    , member => member.DivisionId
    , division => division.DivisionId
    , (a, b) => new
    {
        Member = a,
        Division = b,
    });

foreach (var pair in pairs)
{
    Copy(pair.Member, pair.Division);
}

Note that the Zip will be faster, if the items are already in the proper order. 请注意,如果物品已经按照正确的顺序放置,则Zip会更快。 Join will be slower than the Zip, but will be faster than manually re-ordering the items to allow a Zip . Join将比Zip慢,但比手动重新排序项目以允许Zip快。

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

相关问题 使用LINQ将双精度和整数从一个列表复制到另一列表 - Copy doubles and integers from one list to another using LINQ 使用LINQ将数据从一个列表复制到另一个列表 - Using LINQ to copy data from one list to another 在List中使用Linq查询 <T> .AddRange有条件地将元素从一个列表添加到另一个列表 - Using Linq query inside List<T>.AddRange to conditionally add elements from one list to another 如何使用LINQ C#将几个属性的值从一个列表更改为另一个: - How to change values of few properties from one list to another using LINQ C#: 根据属性将列表对象从一个列表复制到另一个列表 - Copy list objects from one list to another list based on properties 使用LINQ将一个List复制到另一个List和SubList - Copy one List to another List and SubList with LINQ 如何在LINQ中将对象的所有属性复制到另一个属性 - How to copy all properties of an object to another in LINQ C#列表:如何将元素从一个列表复制到另一个列表,但仅复制某些属性 - C# Lists: How to copy elements from one list to another, but only certain properties 使用LINQ将特定行从一个数据表复制到另一数据表 - Copy specific rows from One DataTable to another DataTable using LINQ 使用linq将数据从一个表复制到另一个表的最快方法 - Fastest way to copy data from one table to another using linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM