简体   繁体   中英

Adding two lists together with duplicates C# MVC

I have 3 lists of product entity keys that I want to put together into one big list . I want the duplicates present as I want to determine which product is mentioned most in the final list.

I am currently trying to use .Union but this removes duplicates from the final list.

//Union selector results
IQueryable<ProductModel> unionProducts = categoryProducts.Union(attOptProducts);

if (manufacturerProducts != null) 
    unionProducts = unionProducts.Union(manufacturerProducts);

if (brandProducts != null) 
    unionProducts = unionProducts.Union(brandProducts);

Any help would be appreciated.

Use Concat instead of Union to keep duplicates:

        IQueryable<ProductModel> unionProducts = categoryProducts.Concat(attOptProducts);
        if (manufacturerProducts != null)
            unionProducts = unionProducts.Concat(manufacturerProducts);
        if (brandProducts != null) 
            unionProducts = unionProducts.Concat(brandProducts);

It can also be done by method 方法来完成

IQueryable<ProductModel> unionProducts = categoryProducts.AddRange(attOptProducts);
        if (manufacturerProducts != null) unionProducts = unionProducts.AddRange(manufacturerProducts);
        if (brandProducts != null) unionProducts = unionProducts.AddRange(brandProducts);

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