I have a controller that has a few actions on it, some of them may have a custom attribute. I want to use linq to select some data into an anonymous type for each action on the controller e.g.
Controller1
Action1
[MyAttribute("Con1Action2)"]
Action2
[MyAttribute("Con1Action3")]
Action3
Controller2
Action1
[MyAttribute("Con2Action2)"]
Action2
[MyAttribute("Con2Action3")]
Action3
I want the following returned:
NameSpace = "someText", Controller = "Controller1", ActionName = "Con1Action2",
NameSpace = "someText", Controller = "Controller1", ActionName = "Con1Action3",
NameSpace = "someText", Controller = "Controller2", ActionName = "Con2Action2",
NameSpace = "someText", Controller = "Controller2", ActionName = "Con2Action3"
I'm struggling with a SelectMany for each action:
var controllers= myControllerList
.Where(type =>type.Namespace.StartsWith("X.") &&
type.GetMethods().Any(m => m.GetCustomAttributes(typeof(MyAttribute)).Any()))
.SelectMany(type =>
{
var actionNames = type.GetMethods().Where(m => m.GetCustomAttributes(typeof(MyAttribute)).Any()).ToList();
var actionName = (MyAttribute)actionNames[0].GetCustomAttribute(typeof(MyAttribute));
return new
{
Namespace = GetPath(type.Namespace),
ActionName= actionName.Name,
Controller = type.Name.Remove(2);
};
}).ToList();
I'm getting an error on the SelectMany- the type arguments for the method...cannot be inferred from the usage.