简体   繁体   中英

How do I copy one list to another that has an object with additional properties in C# ? (without a foreach)

I have the need to copy one list of objects into another, however, the second list has additional properties (basically, the first list is a subset of the second list). Here's an example of how to do with a foreach loop for a class Product and another class ProductExt that inherits from product and adds another property to it. -- this is an abbreviated version of what I need to do --

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    //note there are 78 more product properties!
}

public class ProductExt : Product
{
    public int FirstVariantId { get; set; }
}

And an example of how it's done with a foreach (but I'd like to do this in Linq but not really sure how to go about it):

        List<Product> products = new List<Product>();
        products.Add(new Product { Id = 1, Name = "widget a" });
        products.Add(new Product { Id = 2, Name = "widget b" });
        products.Add(new Product { Id = 3, Name = "widget c" });
        products.Add(new Product { Id = 4, Name = "widget d" });
        products.Add(new Product { Id = 5, Name = "widget e" });
        products.Add(new Product { Id = 6, Name = "widget f" });
        products.Add(new Product { Id = 7, Name = "widget g" });

        List<ProductExt> productExts = new List<ProductExt>();
        
        // want to  copy products into productExts list with a default value of 0 for FirstVariantId
       foreach(var p in products)
        {
            var px = new ProductExt();
            px.Id = p.Id;
            px.Name = p.Name;
            px.FirstVariantId = 0;
            productExts.Add(px);
        }

Is there a way to do this in Linq (with the default value)?

Try this:

List<ProductExt> productExts = products.select(product=> new ProductExt()
    {
        Name = product.Name,    
        // another properties
    }).Tolist();

If you don't assign value to any property, the default value set to that property. ( Example: For int properties => 0 )

You can always use AutoMapper for these type of work. The benefit would be that you wouldn't need to remember mapping new properties if you add new ones later.


Create a singleton mapper that you can access from your classes and methods, usually this is done on application startup

 var config = new MapperConfiguration(cfg => { 
    cfg.CreateMap<Product, ProductExt>();
 });
 var mapper = config.CreateMapper();

Then

List<ProductExt> productExts = products.Select(p => mapper.Map(p, new ProductExt())).ToList();

Ok, it looks like Mapster is the winner here - it's very fast and simple.

        List<ProductExt> productExts = new List<ProductExt>();
        products.Adapt(productExts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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