Farzane MH
Farzane MH

Reputation: 11

split a number into an array of strings

There is a list of candidates.

candid: {12,14,16,19,25,64,78}

Code :

    for (int i = 0; i < candid.Count; i++)
    {
        var searchTerm = candid[i].ToString();
        var searchItems = searchTerm.ToCharArray().ToString();
        foreach (Tran b in transactions)
        {
            string[] temp = new string[b.itemsUtilities.Count];
            int j = 0;
            foreach (ItemUtility c in b.itemsUtilities)
            {
                temp[j] = c.item.ToString();
                j = j + 1;
            }
            if (searchItems.All(a => temp.Contains(a)))
                arraye[i] = arraye[i] + (b.transactionUtility);
        }
    }

I receive the following error:

'string[]' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains(IQueryable, char)' requires a receiver of type 'IQueryable'

If code changed from : var searchItems = searchTerm.ToCharArray().ToString();

To : var searchItems = searchTerm.split();

This error is fixed, But this split command does not separate numbers.

Upvotes: 0

Views: 92

Answers (2)

Drag and Drop
Drag and Drop

Reputation: 2734

More that a simple missing include, I do think that you are over thinking

your code seems to be simplify as :

for (int i = 0; i < candid.Count; i++)
{
    var searchTerm = candid[i].ToString();

    foreach (Tran b in transactions)
    {       
        var tmp = b.itemsUtilities.Select(x=> x.item.ToString()).ToList(); 

        if( searchTerm.All(a=> tmp.Contains (a)){
            arraye[i] = arraye[i] + (b.transactionUtility);
        }
    }
}

And you can simplify again instead of for each you filter Trasanction based on the if condition. And if your candid is 11 no need to check twice for 1 so a simple Distinct() will help here.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460018

I guess you want to separate the numbers into a string[].

var searchItems = searchTerm.ToCharArray().ToString();

This will always create a single string "System.Char[]" so is not what you want.

I guess you want:

string[] searchItems = searchTerm.Select(c => c.ToString()).ToArray();

This should fix the compiler error because searchItems.All will now project strings and not chars.

Upvotes: 3

Related Questions