gpt4 book ai didi

c# - 使用 LINQ GroupBy 获取忽略属性的唯一集合

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:19 25 4
gpt4 key购买 nike

使用 Rules 集合,我试图创建另一个 Rules 集合,忽略 Site 属性并创建一个唯一列表。

public class Rule
{
public int TestId { get; set; }
public string File { get; set; }
public string Site { get; set; }
public string[] Columns { get; set; }
}

所以如果我的收藏有如下值:

var rules = new List<Rule>
{
new Rule { TestId = 1, File = "Foo", Site = "SiteA", Columns = new string[] { "ColA", "ColB" }},
new Rule { TestId = 1, File = "Foo", Site = "SiteB", Columns = new string[] { "ColA", "ColB" }}
};

我要的是最终结果

var uniqueRules = new List<Rule>
{
new Rule { TestId = 1, File = "Foo", Site = null, Columns = new string[] { "ColA", "ColB" }}
};

尝试了如下所示的各种组合后,我仍然得到 2 个结果,如何才能达到预期的结果?

var uniqueRules = rules
.GroupBy(r => new { r.TestId, r.File, r.Columns })
.Select(g => g.Key)
.Distinct()
.ToList();

最佳答案

问题是 string[]没有覆盖 EqualsGetHashCode ,这就是为什么只在 r.Columns 比较引用文献的原因.您需要提供自定义 IEqualityComparer<T> :

public class RuleComparer : IEqualityComparer<Rule>
{
public bool Equals(Rule x, Rule y)
{
if (object.ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
if(!(x.TestId == y.TestId && x.File == y.File)) return false;
return x.Columns.SequenceEqual(y.Columns);
}

// from: https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
public int GetHashCode(Rule obj)
{
unchecked
{
int hash = 17;
hash = hash * 23 + obj.TestId.GetHashCode();
hash = hash * 23 + (obj.File?.GetHashCode() ?? 0);
foreach(string s in obj.Columns)
hash = hash * 23 + (s?.GetHashCode() ?? 0);
return hash;
}
}
}

现在 LINQ 查询变得很简单:

List<Rule> uniqueRules = rules.Distinct(new RuleComparer()).ToList();

关于c# - 使用 LINQ GroupBy 获取忽略属性的唯一集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442857/

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