gpt4 book ai didi

带有对象列表的 C# if 语句语法

转载 作者:太空狗 更新时间:2023-10-29 22:07:40 25 4
gpt4 key购买 nike

我有一个对象列表,称为属性,基本上,我需要在 C# 中执行以下操作

<pseudocode>
if (list.Contains(Attribute where getName() == "owner"))
{
do stuff
}
</pseudocode>

我遇到的问题是 if 的嵌套括号位 - "Attribute where getName() == "owner"。这是我的代码 - 它显然不起作用,但大部分 if 应该是是的,只是我需要在正斜杠中做一些事情,但我不知道该怎么做。

if (attributes.Contains(Attribute /where/ attribute.getName() == "Owner"))
{
string value = attr.getValue();
value = value.Replace(domain, "");
user = value;
UserExists(value);
}

我可能太笨了,但我不得不重新开始 3 天的开发以将所有内容更改为使用 Attribute 对象,所以我的大脑相当崩溃。对不起。

最佳答案

如果您使用的是支持 LINQ(3.5 或更高版本)的 .NET 版本,请尝试

if(attributes.Any(attribute=>attribute.getName()=="Owner"))
{
do stuff
}

这有一个很好的优势,即必须维护此代码的任何人都可以很好地阅读。

关于带有对象列表的 C# if 语句语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299856/

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