1

Here is my enum values defined

public enum authaccess
{ read=0,create=1,update=2,delete=4}

As a Access Table looks like as below.

|id  | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1   | cms       |header    |3        |
|2   | cms       |footer    |2        |
|3   | cms       |content   |7        |

read access is permitted for all content, while 3 is for(create + update), and 7 for all rights.

As I need (x) button enabled in div where loggedInUser has delete authority

Using Linq, I used

new DbContext().tbl_access.where(k=>k.permitted > authaccess.delete){divid.class.add('close')}

but could not find a way to get a list of contents that can be changed by user ie 3 or (authaccess.create + authaccess.update)

[Clarification on edit] The resultant table should be

|id  | tablename |columnname|permitted|
|----|-----------|----------|---------|
|1   | cms       |header    |3        |
|2   | cms       |footer    |2        |

Because (header and footer) include create+update level access which is less than or equal to 3.

Thanks in advance for any help.

1
  • Which type of LINQ is this? And if it's LINQ to Entities (Entity Framework), is the enum also mapped as enum? And is is a flagged enum? Commented Dec 17, 2017 at 9:19

2 Answers 2

1

It looks like your authaccess has values for bit masking. For your requested create + update, a possible way to write your Linq is:

new DbContext().tbl_access
    .Where(k => (k.permitted & (int)authaccess.create) > 0) && 
                (k.permitted & (int)authaccess.update) > 0))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I like it this way
0

Do you want something like that;

var authAccessFilter = (int)authaccess.create + (int)authaccess.update;
var records = new DbContext().tbl_access.Where(k => k.permitted <= authAccessFilter).ToList();

2 Comments

sorry, the problem was not clearly understood. I have added few lines below to clarify the requirement.
<= is not recommended, when later values on enum do not override the previous roles.

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.