-1

I have a string like this:

RoleId,RoleName|CategoryId,CategoryName

I split them first like this:

string delm = "RoleId,RoleName|CategoryId,CategoryName";

string[] FieldsToReplace = attributes[0].IdsToReplaceWith.Split('|');

Suppose i have a variable in which i have RoleId:

string test = "RoleId";

Now what i am trying to get each the array item in which has string RoleId, i don't want to use contains i need exact match.

I have tried this query:

        var test = FieldsToReplace
                   .Where(x=>FieldsToReplace
                   .All(y => y.Split(',').Equals(delm))).ToArray();

i can harcode like this for first index:

var IdProperty = FieldsToReplace.FirstOrDefault(x => x.Split(',')[0] == delm);

but i want it dynamic so it check each item of array which i got after , split.

but it returns no record.

Any help will be appreciated.

Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
  • y.Split(',') returns an array. Maybe you need y.Split(',')[1] ? – Dennis_E Jun 25 '14 at 11:28
  • I'm not sure I follow.. What's the expected output? – dcastro Jun 25 '14 at 11:28
  • output is to reutrn that array item which has Roleid exact matched, actually i need to split every item of array on ``,`` and then check if matches with RoleId, if matches return that item of array – Ehsan Sajjad Jun 25 '14 at 11:40
  • why i am getting downvotes my question is genuine and i am not able to solve my issue – Ehsan Sajjad Jun 25 '14 at 11:41
  • When you say return that item of the array, you're not making sense. The array you're searching through, if split again on a `,`, when matched simply returns the string you're matching. – Mike Perrenoud Jun 25 '14 at 11:48
  • There's not much point returning the thing you're looking for. Do you mean you want the index number for the item? – Enigmativity Jun 25 '14 at 11:48
  • @Enigmativity every item has delimeted string and i want to split on '','' and check if it matches my required string if matches return the main array item in which it is matching in RoleId case index 0 of arrray – Ehsan Sajjad Jun 25 '14 at 12:05

1 Answers1

2

You want to split on your elements of the array. Besides that it seems appropriate to check if any element of these splitted ones are equal to your comparison string:

var test =
    FieldsToReplace
        .Where(x => x.Split(',')
            .Any(y => y.Equals(prop.Name)))
        .ToArray();
Enigmativity
  • 97,521
  • 11
  • 78
  • 153
EKrueger
  • 700
  • 8
  • 20