0

I am trying to check the int values in an array and based on that do some calculation but the code is not working below is the code:

string EventIds = getVoucher.EventIDs;

int[] array = EventIds.Split(',')
                      .Select(x => int.Parse(x, CultureInfo.InvariantCulture))
                      .ToArray();

if(array.ToString().Any(s => booking.EventID.ToString().Contains(s)))
{do something; } else { do something;}
4
  • if I dont convert array into string then I cant use LInq expression 'Any' nd 'Contains' Commented Aug 10, 2011 at 15:34
  • 1
    You'll have to tell us what you want it to do that it isn't, and what it is doing that you wish it wouldn't... :) Commented Aug 10, 2011 at 15:35
  • the array[] contains the value for instance 23 , 24,25 .. I want to check that with the EventId value passed which is an integer (1,2,3..), If an array contains the Event ID then it should do some calculation else throw an error , hope it make sense Commented Aug 10, 2011 at 15:37
  • 1
    That makes a lot more sense. I think @dtb's answer is what you're looking for. Commented Aug 10, 2011 at 15:40

5 Answers 5

3

array.ToString returns the string "System.Int32[]". Using Any with a string checks the predicate for each character in the string.

Assuming that booking.EventID is an int such as 1234, booking.EventID.ToString() returns the string "1234".

So your code checks if "1234" contains any character in "System.Int32[]" (here: true, because "1234" contains the '3' of "System.Int32[]").


You don't say what the desired result is, but I guess you're looking for something like this:

if (array.Any(s => booking.EventID == s))
{
    // ...
}

or

if (Array.IndexOf(array, booking.EventID) != -1)
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1
// cache it to avoid multiple time casting
string bookingId = booking.EventID.ToString();

// you can do filtering in the source array without converting it itno the numbers
// as long as you won't have an Exception in case when one of the Ids is not a number
if(EventIds.Split(',').Any(s => bookingId.Contains(s)))
{
  // ..
}
else
{
 // ...
}

Also, depends on how source array is generated you should consider Strign.Trim() to remove spaces:

if(EventIds.Split(',').Any(s => bookingId.Contains(s.Trim())))

Comments

1

Why you try to convert to string array?

array.ToString();//???

this code will return System.Int32[]

remove the ToString()!!! if you want to enumerate the array use this code instead

array.AsEnumerable().Any(...

1 Comment

I have added comment which will make it more clear to understand what i m looking for
1

Try this,

if (
        EventIds.Split(',').OfType<string>()
            .Any(e => booking.EventID.ToString().Contains(e))
    )
{
    //Some member of a comma delimited list is part of a booking eventID ???
}
else
{
    //Or Not
}

If this is not what you wanted to do then your code is wrong.

EDIT:

After reading you comment I think you want the more logical

If (EventIDs.Split(',').Select(s => 
    int.Parse(s)).OfType<int>().Contains(booking.EventID))
{
    //Ther booking ID is in the list
}
else
{
    //It isn't
}

Comments

0

Instead of doing a "ToArray()", try doing a "ToList()". you can than use the "Contains" method to search.

Comments

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.