I have the following code that I have been working on:
var menuItems = _contentRepository.GetPk("01" + pk + "000")
.OrderBy(m => m.Order)
.Select(m => new Menu.Item
{
PartitionKey = m.PartitionKey,
RowKey = m.RowKey,
Order = m.Order,
Title = m.Title,
Type = m.Type,
Link = m.Link,
TextLength = m.TextLength
});
One of the fields that is returned .GetPk is a field called Roles. This is a string field with contents that show the roles that can access each item of the menu. The data in the field looks like the following:
All
All,Admin
All,Admin, Super
Admin
Admin, Super
I have a string[] roles that is populated with the roles that a user has. If a user has one role then there’s one item in the string array, if a user has two roles there are two items. If the user has no role then the array is empty.
How can I make it so my select only returns a value if an entry in the string[] roles matches one of the words in my Roles field?
So far I have been doing some research and I found the following which does almost what I need. But how can I fit this into my Linq above?
String[] roles = Roles.GetRolesForUser();
var links = from item in menus
where item.Roles.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Any(x => roles.Contains(x) || x == "All")
select item;
Just include that condition in a Where clause before you Select the results into the form you need them.