C# LINQ迭代IEnumerable并将重复项设置为null

C# LINQ迭代IEnumerable并将重复项设置为null,c#,linq,C#,Linq,我有一个IEnumerable IEnumerable<Pets> pets; 我想迭代这个过程,找到所有重复的价格,并将这些重复的价格设置为null 假设一只猫和一只狗的价格相同:10.55美元。我想保留第一个,但删除所有剩余的价格。方法: 1) 删除重复项(我建议): 2) Sort&evalute-按您的意愿设置null以代替重复项(是否确实要设置null而不是像1中那样删除) 正如D Stanley所注意到的(但我没有注意到),您可能必须将Price设置为null,而不是整

我有一个
IEnumerable

IEnumerable<Pets> pets;
我想迭代这个过程,找到所有重复的价格,并将这些重复的价格设置为null

假设一只猫和一只狗的价格相同:10.55美元。我想保留第一个,但删除所有剩余的价格。

方法:

1) 删除重复项(我建议):

2) Sort&evalute-按您的意愿设置null以代替重复项(是否确实要设置null而不是像1中那样删除)

正如D Stanley所注意到的(但我没有注意到),您可能必须将
Price
设置为
null
,而不是整个记录。然后简单地将其更改为
decimal?
,然后编写
newPets[i]。Price=null而不是整条记录为空。

方法:

1) 删除重复项(我建议):

2) Sort&evalute-按您的意愿设置null以代替重复项(是否确实要设置null而不是像1中那样删除)


正如D Stanley所注意到的(但我没有注意到),您可能必须将
Price
设置为
null
,而不是整个记录。然后简单地将其更改为
decimal?
,然后编写
newPets[i]。Price=null
而不是整条记录为空。

对于初学者来说,十进制数不能为空,所以我会像键入
十进制数一样回答它,这样您就理解了过程

Linq用于查询,而不是更新。您可以基于原始集合投影新集合,但是
foreach
可能更合适:

// list to keep tack of found prices
var prices = new List<decimal>();
foreach(Pet pet in pets)
{
    if(prices.Contains(pet.Price.Value))
        // price was found - set this one to null
        pet.Price = null;
    else
        // add to the list of "found" prices
        prices.Add(pet.Price.Value);
}
//记录找到的价格的列表
var价格=新列表();
foreach(宠物中的宠物)
{
if(prices.Contains(pet.Price.Value))
//找到价格-将此价格设置为空
pet.Price=null;
其他的
//添加到“找到的”价格列表中
价格。增加(宠物价格。价值);
}

对于初学者来说,
十进制数不能为空,所以我会像回答
十进制数一样回答它?
这样你就能理解这个过程

Linq用于查询,而不是更新。您可以基于原始集合投影新集合,但是
foreach
可能更合适:

// list to keep tack of found prices
var prices = new List<decimal>();
foreach(Pet pet in pets)
{
    if(prices.Contains(pet.Price.Value))
        // price was found - set this one to null
        pet.Price = null;
    else
        // add to the list of "found" prices
        prices.Add(pet.Price.Value);
}
//记录找到的价格的列表
var价格=新列表();
foreach(宠物中的宠物)
{
if(prices.Contains(pet.Price.Value))
//找到价格-将此价格设置为空
pet.Price=null;
其他的
//添加到“找到的”价格列表中
价格。增加(宠物价格。价值);
}
请注意,价格现在可以为空(
decimal?

请注意,价格现在可以为空(
decimal?


为了解决这个问题,你做了哪些研究?您尝试过哪些尝试性的解决方案,这些解决方案有哪些问题?我发现很多解决方案都没有列出与IEnumerable相匹配的内容。感谢您的否决票。“删除”和“将价格设置为空”是两件不同的事情-到底是什么?@a
decimal
不能为
null
-您需要将其更改为
decimal?
或选择一个“神奇”值(使用0会导致任何问题吗?使用-1如何?)将
Price
设置为
null
总体上有什么影响?(例如,如果您只是用每个价格“提取”第一个项目,效果会更好吗)?您应该通过示例输入和所需输出来改进此问题,将duplicates设置为null仍然很模糊,是否要将数组/列表中的重复对象设置为null?或者将重复对象的价格设置为空?还是别的什么?或者读者不得不猜测?为了找到解决这个问题的方法,你做了哪些研究?您尝试过哪些尝试性的解决方案,这些解决方案有哪些问题?我发现很多解决方案都没有列出与IEnumerable相匹配的内容。感谢您的否决票。“删除”和“将价格设置为空”是两件不同的事情-到底是什么?@a
decimal
不能为
null
-您需要将其更改为
decimal?
或选择一个“神奇”值(使用0会导致任何问题吗?使用-1如何?)将
Price
设置为
null
总体上有什么影响?(例如,如果您只是用每个价格“提取”第一个项目,效果会更好吗)?您应该通过示例输入和所需输出来改进此问题,将duplicates设置为null仍然很模糊,是否要将数组/列表中的重复对象设置为null?或者将重复对象的价格设置为空?还是别的什么?或者读者不得不猜测?作为第三种变体使用,作为第三种变体使用我认为更好的
HashSet
,简单地列出:-)@Grundy以获得性能?当然我更感兴趣的是演示这个过程,而不是给出优化的代码。我认为最好的
HashSet
就是简单地列出:-)@Grundy来提高性能?当然我更感兴趣的是演示这个过程,而不是给出优化的代码。
var newPets = pets.OrderBy(per => pet.Price).ToList();
if (!newPets.Any()) return newPets;
var last = 0;
for (var i = 1; i < newPets.Count; i++) 
{
     if (newPets[i].Price == newPets[last].Price) newPets[i] = null;
     else last = i;
}

return newPets;
var newPets = pets.ToList();
for (var i = 0; i < newPets.Count; i++)
{
    if (newPets[i] == null) continue;
    var price = newPets[i].Price;
    for (var j = i + 1; j < newPets.Count; j++)
    {
        if (newPets[j].Price == price) newPets[j] = null;
    }
}
// list to keep tack of found prices
var prices = new List<decimal>();
foreach(Pet pet in pets)
{
    if(prices.Contains(pet.Price.Value))
        // price was found - set this one to null
        pet.Price = null;
    else
        // add to the list of "found" prices
        prices.Add(pet.Price.Value);
}
public class Pet
{
    public string Name { get; set; }
    public string Other { get; set; }
    public decimal? Price { get; set; }
}
return pets
    .OrderBy(x => x.Name)
    .GroupBy(x => x.Price)
    .OrderBy(x => x.Key)
    .SelectMany(x => (new[] { x.First() }).Union(x.Skip(1).Select(n => new Pet { Name = n.Name, Other = n.Other, Price = null })))
    .ToList();