1

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!

2
  • 1
    @abatishchev: The question clearly says "I am using VS 2008, 3.5 framework". It was originally tagged with C# 3.0, which is not the same thing as .NET 3.0. I don't understand how either your edit or your comment applies. Commented Apr 21, 2011 at 12:37
  • 1
    @Cody: Thank you, and sorry. Anyway, be easier, nothing serious happened. Cheers! Commented Apr 21, 2011 at 12:40

1 Answer 1

6

You could use the overload of Any that takes a predicate:

bool valueIsPresent = client.Any(c => c.TIN == anyNumber);
Sign up to request clarification or add additional context in comments.

1 Comment

thx. The solution was staring at my face and i did not realize. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.