I have a controller on which there are several actions, some of them may have their own attribute. I want to use linq to select some data in an anonymous type for each action on the controller, for example.
Controller1
Action1
[MyAttribute("Con1Action2)"]
Action2
[MyAttribute("Con1Action3")]
Action3
Controller2
Action1
[MyAttribute("Con2Action2)"]
Action2
[MyAttribute("Con2Action3")]
Action3
I want the following to return:
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 struggle with SelectMany for every 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 get an error in SelectMany - type arguments for a method ... cannot be taken out of use.
source
share