Marks
Marks

Reputation: 79

Linq get rows by enum values

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.

Upvotes: 1

Views: 306

Answers (2)

Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

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();

Upvotes: 0

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

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))

Upvotes: 1

Related Questions