签入对象数组
我有一个对象数组,例如 ClientsDTO[]
。对象中有一个名为 Tin
的属性。我想检查数组中是否存在某个值。我有一种检查方法,即使用 LINQ 查询并获取满足条件的对象列表。如果返回的列表为 null,则该值不存在于数组中。如果列表存在,则值存在于数组中。以下是示例代码。
我想知道是否有比这更好的方法,即更优雅且性能更好的方法。我使用的是VS 2008,3.5框架。
ClientsDTO[] client = new ClientsDTO[10];
var lstclient = client.Where(c => c.TIN == anyNumber).FirstOrDefault();
if (lstclient == null)
{
//value present in array
}
else
{
//value not present
}
提前致谢!
I have an array of an object say ClientsDTO[]
. There is a property namely Tin
in the object. I want to check if a value is present in the array. I have one way of checking this i.e. using the LINQ query and getting the list of objects which satisfies the condition. If the returned list is null then the value is not present in the array. If the list exists then value is present in the array. The following is a sample code.
I want to know is there a better way than this i.e. something more elegant and with better performance. I am using VS 2008, 3.5 framework.
ClientsDTO[] client = new ClientsDTO[10];
var lstclient = client.Where(c => c.TIN == anyNumber).FirstOrDefault();
if (lstclient == null)
{
//value present in array
}
else
{
//value not present
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Any
的重载采用谓词:You could use the overload of
Any
that takes a predicate: