简体   繁体   English

如果出现T的变化,则从List <T>中删除所有项目

[英]Remove all items from a List<T> if variations of T occur

been struggling with this for a while now. 现在已经挣扎了一段时间。

I'm dipping my toe in the WebAPI world and I have a List that can contains products with the same name but different prices. 我在WebAPI世界中蘸着我的脚趾,我有一个List,可以包含同名但价格不同的产品。 What I need to do is remove all references to a product is variations in price occur. 我需要做的是删除所有对产品的引用是出现价格变化。

eg. 例如。
name = "Cornflakes" Price = "1.99M" name =“Cornflakes”价格=“1.99M”
name = "Cornflakes" Price = "1.89M" name =“Cornflakes”价格=“1.89M”
name = "Rice Krispies" Price = "2.09M" name =“Rice Krispies”价格=“2.09M”
name = "Cornflakes" Price = "2.09M" name =“Cornflakes”价格=“2.09M”

No cornflakes should appear in my final list. 我的最终名单中不应出现任何玉米片。

I've got the bulk written but it's removing the products too soon and I'm unsure where I should be removing them. 我已经写了大量的,但它很快就删除了产品,我不确定我应该删除它们。

public IEnumerable<Product> GetProductsByCategory(int Id)
    {
        List<Product> sourceProductList = products.Where(p => p.CategoryID == Id).ToList();
        List<Product> tempProducts = new List<Product>();
        List<Product> targetProductList = new List<Product>();

        foreach (var product in sourceProductList)
        {
            bool isInTempList = tempProducts.Any(x => x.Name == product.Name);
            if (!isInTempList)
            {
                tempProducts.Add(product);
            }
            else
            {
                Product tempProduct = product;
                bool isPriceDifferent = tempProducts.Where(y => y.Name == tempProduct.Name).Any(y => y.Price != tempProduct.Price);
                if (isPriceDifferent)
                {
                    tempProducts.RemoveAll(p => p.Name == product.Name); 
                    // too soon as I may have lots of products with the same name
                    // but need to remove based on product.Name
                }
            }
        }
        targetProductList.AddRange(tempProducts);

        return targetProductList;
    }

Any help would be greatly appreciated. 任何帮助将不胜感激。

Note: other cereals are available 注意:其他谷物可供选择

Try this LINQ expression that will only select the products that have one distinct price: 试试这个LINQ表达式,它只选择具有一个不同价格的产品:

var result = sourceProductList
    .GroupBy(x => x.Name)
    .Where(g => g.Select(x => x.Price).Distinct().Count() == 1)
    .Select(g => g.First());

See it working online: ideone . 看到它在线工作: ideone

Please try this: 请试试这个:

class Program
    {
        static void Main(string[] args)
        {
            var list = new List<Product>
                {
                    new Product() {Name = "Cornflakes", Price = 100},
                    new Product() {Name = "Cornflakes", Price = 200},
                    new Product() {Name = "Rice Krispies", Price = 300},
                    new Product() {Name = "Cornflakes", Price = 400}
                };

            var uniqueItems = list.Where(w => (!list.Any(l=>l.Name.Equals(w.Name) && l != w)));

        }

        public class Product
        {

            public string Name { get; set; }
            public decimal Price { get; set; }
        }
    }

In the result you will have only one "Rice Krispies" item. 在结果中,您将只有一个“Rice Krispies”项目。 I'm sure it will work faster than solution with GroupBy and Distinct, because we don't need to do these unnecessary things in your case. 我确信它比GroupBy和Distinct的解决方案更快,因为我们不需要在你的情况下做这些不必要的事情。

Working Code - http://ideone.com/X8A3v 工作代码 - http://ideone.com/X8A3v

Something like this (freehand so may be slightly wrong syntax): 像这样的东西(写意可能会略微错误的语法):

var toRemove = sourceProductList
    .GroupBy(p => p.Name)
    .Where(g => g.Count() > 1)
    .SelectMany(g => g)
    .GroupBy(p => p.Price)
    .Where(g => g.Count() > 1)
    .SelectMany(g => g.Select(p => p.ID))
    .Distinct()
    .ToList();
toRemove.ForEach(id => sourceProductList.RemoveAll(p => p.ID == id));

This should be as easy as grouping by name, getting only those where only 1 item exists in the groups: 这应该像按名称分组一样简单,只获取组中只存在1个项目的那些:

var filtered = list.GroupBy(i => i.Name)
      .Where(i => i.Count() == 1)
      .SelectMany(x => x)

Live example: http://rextester.com/AUBOHU96105 实例: http//rextester.com/AUBOHU96105

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

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