Link to home
Create AccountLog in
Avatar of sbornstein2
sbornstein2

asked on

C# + Simple List to Array question

Hello all,

I have a List called List<Roles> and in this list there is the following columns:

RoleCode (string) - ie. ABC
ActionCreate (bool)  - ie. true
ActionUpdate (bool) - ie. true
ActionDelete (bool)  - ie. false
ActionAssign (bool)  - ie. false

What I want to do is populate a string array as follows:
Need to check if the action is true then create the string array such as using the ie. example.

ABC-Create, ABC-Update

So I want to append a dash between the rolecode and action stripping the word action for only those that are true.  Not sure Linq can be used in thie case.   Also was hoping to be able to keep it where if I added a new action later I would not have to hard code for it.

Thanks all.
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Your question is not very clear.

As I understand you have a List where each item is an array of strings. Am I right?

What is not clear why you want to concatenate role code VALUE with action NAME, not value?

Could you please explain more clear?

What is <Roles> in the list definition?

What's on me, I'd create a class <Role> class with five fields as above. And you may add another field when you want. From an object you may retrieve all the information you need.
Avatar of sbornstein2
sbornstein2

ASKER

No so I have a List already I need to convert it into a string array taking each of the action columns and appending it to the role code.  For example:

List<UserInRole> roles =
                UserInRoleService.FindAllUsersInRole().Where(p => p.UserID == GetCurrentUser().UserID
                                                             && p.IsActive).ToList();

   var arrRoles = new List<string>();

            foreach (UserInRole ur in roles)
            {

}

so I can get the values of the List item of course through the loop ur.RoleCode ur.ActionCreate etc.  I was hoping a cleaner way but  I could always say if(ur.ActionCreate) etc.  Maybe not really a cleaner way to do it
I guess I could do this but was hoping to have a cleaner way.

            var arrRoles = new List<string>();

            foreach (UserInRole ur in roles)
            {
                if (ur.Role.ActionView)
                    arrRoles.Add(ur.Role.RoleCode + "-" + "View");

                if (ur.Role.ActionCreate)
                    arrRoles.Add(ur.Role.RoleCode + "-" + "Create");

                if (ur.Role.ActionUpdate)
                    arrRoles.Add(ur.Role.RoleCode + "-" + "Update");
            }
I was wanting to be able to handle any Action column I guess but it is a property in the List.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Just remember - Reflection is pretty slow process. So, if you need high performance this may be a problem.
awesome thanks
I'm pretty certain there's a way to speed this up with some initial Expression Trees, but I have to get into a zen state of mind to tackle those.  However, there's another approach to using Reflection available in this case.  The code I'm attaching should hopefully explain it.

Basically, what I'm doing in this case is doing the reflection once, then storing a cache of the PropertyInfo values in a static List<> for later retrieval by other instances of the same class.

I've modified your code just a little bit for benchmarking purposes.  There's no real testing harness, just a handful of methods that need to be swapped out to get the timing values.  However, your original method minus the output is in the method "TestValuesOriginal()".  I refactored it in method "TestValuesRefactor1()" a bit to use LINQ heavily and it slowed down significantly at about a 75% increase in time.  I then modified that for the mthod "TestValuesRefactor2()" and got about a 25% decrease in time from the original method.

On my machine, the results for each method averaged as follows:
TestValuesOriginal: .008ms
TestValuesRefactor1: .014ms
TestValuesRefactor2: .006ms
Q-28135005.cs