Linq get rows by enum values

时间:2017-12-17 08:18:43

标签: c# asp.net linq enums

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.

2 个答案:

答案 0 :(得分:1)

看起来您的authaccess具有位屏蔽的值。对于您请求的create + update,编写Linq的可能方法是:

new DbContext().tbl_access
    .Where(k => (k.permitted & (int)authaccess.create) > 0) && 
                (k.permitted & (int)authaccess.update) > 0))

答案 1 :(得分:0)

你想要那样的东西;

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