0

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.

7
  • 5
    This doesn't make any sense: searchTerm.ToCharArray().ToString(). it will always return "System.Char[]" no matter what searchTerm was. Commented May 17, 2018 at 13:45
  • using system.Linq are you missing this Commented May 17, 2018 at 13:46
  • 1
    I believe you want to have a string[] searchItems but ended getting a string searchItems because of the .ToCharArray().ToString() Commented May 17, 2018 at 13:49
  • @Rahul The problem is bigger, the error message could be considered secondary in this particular problem Commented May 17, 2018 at 13:53
  • From my understanding searchTerm.All(a => temp.Contains(a)) will work too no need to cast a string into a string [] Commented May 17, 2018 at 13:54

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The dangers of using var xD
0

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.

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.