2

String Array 1: (In this format: <MENU>|<Not Served?>|<Alternate item served>)

Burger|True|Sandwich
Pizza|True|Hot Dog

String Array 2: (Contains Menu)

Burger
Pizza
Grill Chicken
Pasta

I need the menu is served or any alternate item served for that particular item.

Code:

for(int i = 0; i < strArr2.Length; i++)
{
    if(strArr2.Any(_r => _r.Split('|').Any(_rS => _rS.Contains(strArr1[i])))) 
    {
        var menu = strArr2[i];
        var alternate = ? // need to get alternate item
    }
}

As I commented in the code, how to get the alternate item in that string array? Please help, thanks in advance.

P.S: Any help to trim if condition is also gladly welcome.

2
  • What does each of the arrays represent? Commented Dec 11, 2016 at 7:31
  • First array represents only the Menu, and Alternate item served for that menu. Second array represents overall menu. Commented Dec 11, 2016 at 7:33

3 Answers 3

1

Instead of any, you may use Where to get the value matching.

@Markus is having the detailed answer, I am just using your code to find a quick fix for you.

for(int i = 0; i < strArr2.Length; i++)
{
    if(strArr2.Any(_r => _r.Split('|').Any(_rS => _rS.Contains(strArr1[i])))) 
    {
        var menu = strArr2[i];
        var alternate = strArr2.Where(_rs => _rs.Split('|').Any(_rS => _rS.Contains(strArr1[i]))).First().Split('|').Last();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In order to simplify your code, it is a good idea to better separate the tasks. For instance, it will be much easier to handle the contents of string array 1 after you have converted the contents into objects, e.g.

class NotServedMenu
{
    public string Menu { get; set; }
    public bool NotServed { get; set; }
    public string AlternateMenu { get; set; }
}

Instead of having an array of strings, you can read the strings to a list first:

private IEnumerable<NotServedMenu> NotServedMenusFromStrings(IEnumerable<string> strings)
{
    return (from x in strings select ParseNotServedMenuFromString(x)).ToArray();
}

private NotServedMenu ParseNotServedMenuFromString(string str)
{
    var parts = str.Split('|');
    // Validate
    if (parts.Length != 3)
        throw new ArgumentException(string.Format("Unable to parse \"{0}\" to an object of type {1}", str, typeof(NotServedMenu).FullName));
    bool notServedVal;
    if (!bool.TryParse(parts[1], out notServedVal))
        throw new ArgumentException(string.Format("Unable to read bool value from \"{0}\" in string \"{1}\".", parts[1], str));
    // Create object
    return new NotServedMenu() { Menu = parts[0], 
                                 NotServed = notServedVal, 
                                 AlternateMenu = parts[2] }; 
}

Once you can use the objects, the subsequent code will be much cleaner to read:

var notServedMenusStr = new[] 
{
     "Burger|True|Sandwich", 
     "Pizza|True|Hot Dog"
};
var notServedMenus = NotServedMenusFromStrings(notServedMenusStr);
var menus = new[] 
{
     "Burger", 
     "Pizza", 
     "Grill Chicken", 
     "Pasta"
};
var alternateMenus = (from m in menus join n in notServedMenus on m equals n.Menu select n);
foreach(var m in alternateMenus)
    Console.WriteLine("{0}, {1}, {2}", m.Menu, m.NotServed, m.AlternateMenu);

In this sample, I've used a Linq join to find the matching items.

1 Comment

Explained in detail code and made me understand in easy way! Brilliant.
0

You could do something like that

string[] strArr1 = { "Burger|True|Sandwich", "Pizza|True|Hot Dog" };
string[] strArr2 = { "Burger", "Pizza", "Grill Chicken", "Pasta" };

foreach (string str2 in strArr2)
{
    string str1 = strArr1.FirstOrDefault(str => str.Contains(str2));
    if (str1 != null)
    {
        string[] splited = str1.Split('|');
        string first = splited[0];
        bool condition = Convert.ToBoolean(splited[1]);
        string second = splited[2];
     }
}

1 Comment

string str1 = strArr1.FirstOrDefault(str => str.Contains(str2)); //str1 is always null.

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.