C# 从列表中删除所有项目<;T>;如果T发生变化
我已经为此挣扎了一段时间了 我正在涉足WebAPI世界,我有一个列表,其中可以包含同名但价格不同的产品。我需要做的是删除对产品的所有引用,因为价格会发生变化 例如C# 从列表中删除所有项目<;T>;如果T发生变化,c#,asp.net,asp.net-mvc,lambda,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Lambda,Asp.net Web Api,我已经为此挣扎了一段时间了 我正在涉足WebAPI世界,我有一个列表,其中可以包含同名但价格不同的产品。我需要做的是删除对产品的所有引用,因为价格会发生变化 例如 name=“玉米片”Price=“1.99M” name=“玉米片”Price=“1.89M” name=“Rice Krispies”Price=“2.09M” name=“玉米片”Price=“2.09M” 否玉米片应该出现在我的最终列表中 我已经写了大量的,但它删除的产品太快,我不确定我应该删除他们 public IEnumer
name=“玉米片”Price=“1.99M”
name=“玉米片”Price=“1.89M”
name=“Rice Krispies”Price=“2.09M”
name=“玉米片”Price=“2.09M” 否玉米片应该出现在我的最终列表中 我已经写了大量的,但它删除的产品太快,我不确定我应该删除他们
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;
}
public IEnumerable GetProductsByCategory(int-Id)
{
List sourceProductList=products.Where(p=>p.CategoryID==Id).ToList();
List tempProducts=新列表();
List targetProductList=新列表();
foreach(sourceProductList中的var产品)
{
bool isInTempList=tempProducts.Any(x=>x.Name==product.Name);
如果(!isInTempList)
{
添加(产品);
}
其他的
{
产品=产品;
bool isPriceDifferent=tempProducts.Where(y=>y.Name==tempProduct.Name).Any(y=>y.Price!=tempProduct.Price);
如果(isPriceDifferent)
{
tempProducts.RemoveAll(p=>p.Name==product.Name);
//太快了,因为我可能有很多同名产品
//但需要根据product.Name删除
}
}
}
targetProductList.AddRange(tempProducts);
返回targetProductList;
}
任何帮助都将不胜感激
注意:还有其他谷物可用请尝试此LINQ表达式,它将仅选择具有一个不同价格的产品:
var result = sourceProductList
.GroupBy(x => x.Name)
.Where(g => g.Select(x => x.Price).Distinct().Count() == 1)
.Select(g => g.First());
在线查看它的工作情况:。类似这样的内容(徒手,所以可能语法有点错误):
这应该与按名称分组一样简单,只获取组中仅存在1项的组:
var filtered = list.GroupBy(i => i.Name)
.Where(i => i.Count() == 1)
.SelectMany(x => x)
现场示例:请尝试以下操作:
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; }
}
}
类程序
{
静态void Main(字符串[]参数)
{
变量列表=新列表
{
新产品(){Name=“玉米片”,价格=100},
新产品(){Name=“玉米片”,价格=200},
新产品(){Name=“Rice Krispies”,价格=300},
新产品(){Name=“玉米片”,价格=400}
};
var uniqueItems=list.Where(w=>(!list.Any(l=>l.Name.Equals(w.Name)&&l!=w));
}
公共类产品
{
公共字符串名称{get;set;}
公共十进制价格{get;set;}
}
}
结果,你将只有一个“米脆饼”项目。我相信它比GroupBy和Distinct的解决方案工作得更快,因为在您的情况下,我们不需要做这些不必要的事情
工作代码-太棒了!谢谢你,看来我需要再温习一下我的LINQ了。@MarkByers我保证这永远不会发生。
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; }
}
}