gpt4 book ai didi

c# - 比较列表以检索不匹配的对象属性

转载 作者:行者123 更新时间:2023-11-30 19:59:48 25 4
gpt4 key购买 nike

我正在使用此 linq 查询(在大多数情况下成功)来检查一个列表与另一个列表。目标是返回具有尚未包含在 validThresholds 列表中的 ThresholdType 的对象。

// Filter out thresholds previously added
manufacturerResult = (from r in validThresholds
from t in manufacturerLevel
where r.ThresholdType != t.ThresholdType
select t).ToList();

出于某种原因,这让我很困惑,我得到了 20 个对象的结果。通过将可能的 9 个对象列表与 3 个有效对象列表进行比较。显然我在这里的逻辑上犯了一些错误,但我看不出我怎么能得到比两个集合的总和更多的结果!我真的很想知道为什么会得到这个结果。

最初我想使用 Join 但它限制了我,因为它只允许 equals 没有不等运算符。

下面我创建了一个重现问题的示例应用程序。

public class ConsumableThreshold
{
public int ThresholdType { get; set; }
public int ThresholdValue { get; set; }

public int ConsumableType { get; set; }
public int ConsumableVariantID { get; set; }
public int ManufacturerID { get; set; }
public int ModelID { get; set; }

}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

DoWork();
}

private void DoWork()
{
try
{
int manufacturerID = 4;


ConsumableThreshold t1 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 0,
ThresholdType = 3,
ThresholdValue = 30
};
ConsumableThreshold t2 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 0,
ThresholdType = 2,
ThresholdValue = 50000
};
ConsumableThreshold t3 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 0,
ThresholdType = 6,
ThresholdValue = 3
};
ConsumableThreshold t4 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 2058,
ManufacturerID = 4,
ModelID = 123,
ThresholdType = 3,
ThresholdValue = 31
};
ConsumableThreshold t5 = new ConsumableThreshold()
{
ConsumableType = 3,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 0,
ThresholdType = 3,
ThresholdValue = 99
};
ConsumableThreshold t6 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 123,
ThresholdType = 3,
ThresholdValue = 25
};
ConsumableThreshold t7 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 123,
ThresholdType = 1,
ThresholdValue = 10
};
ConsumableThreshold t8 = new ConsumableThreshold()
{
ConsumableType = 0,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 123,
ThresholdType = 4,
ThresholdValue = 15
};
ConsumableThreshold t9 = new ConsumableThreshold()
{
ConsumableType = 3,
ConsumableVariantID = 0,
ManufacturerID = 0,
ModelID = 0,
ThresholdType = 3,
ThresholdValue = 1
};
ConsumableThreshold t10 = new ConsumableThreshold()
{
ConsumableType = 2057,
ConsumableVariantID = 0,
ManufacturerID = 4,
ModelID = 123,
ThresholdType = 3,
ThresholdValue = 32
};


List<ConsumableThreshold> groupThresholds = new List<ConsumableThreshold>()
{
t1,t2,t3,t4,t5,t6,t7,t8,t9,t10
};

List<ConsumableThreshold> validThresholds = new List<ConsumableThreshold>()
{
t5, t7, t8
};

List<ConsumableThreshold> manufacturerLevel =
(from t in groupThresholds
where t.ManufacturerID != 0 && t.ManufacturerID == manufacturerID
select t).ToList();

if (manufacturerLevel.Count > 0)
{
List<ConsumableThreshold> manufacturerResult = new List<ConsumableThreshold>();

if (validThresholds.Count > 0)
{
// Filter out thresholds previously added
manufacturerResult = (from r in validThresholds
from t in manufacturerLevel
where r.ThresholdType != t.ThresholdType
select t).ToList();
}
else
{
manufacturerResult = manufacturerLevel;
}

validThresholds.AddRange(manufacturerResult);
}
}
catch (Exception)
{

throw;
}
}
}

如何正确比较两个列表并返回另一个列表中不存在特定属性值的对象?

最佳答案

How can I correctly compare two lists and return the objects where a specific property value does not exist in the other?

是这样的吗?

manufacturerResult = (from t in manufacturerLevel
// where a specific property value does not exist in the other
where !validThresholds.Any(
r => r.ThresholdType == t.ThresholdType)
select t).ToList();

My intent is to add the objects from manufacturerLevel to validThresholds where any object in validThreshold doesn't have the same ThresholdType value as any object in manufacturerLevel

validThresholds.AddRange(
manufacturerLevel.Where(t => !validThresholds.Any(
r => r.ThresholdType == t.ThresholdType)));

您可能还想考虑您真正想要做的是否是编译 Set这些对象。如果你做了 validThresholds一个HashSet<> ,例如,使用 IEqualityComparer<>基于 ThresholdType , 你可以简单地 Add所有项目,它会自动忽略任何 ThresholdType已经出现在集合中。

关于c# - 比较列表以检索不匹配的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com