Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Enumerable.where选择要列出的EnumerableIterator<;字符串>;_C#_.net_Linq_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 从Enumerable.where选择要列出的EnumerableIterator<;字符串>;

C# 从Enumerable.where选择要列出的EnumerableIterator<;字符串>;,c#,.net,linq,asp.net-core,asp.net-core-mvc,C#,.net,Linq,Asp.net Core,Asp.net Core Mvc,我需要为一个方法提供一组类型List,这些类型来自apiscription对象: public class AddTags : IOperationFilter { public void Apply(Operation operation, OperationFilterContext context) { if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methods) == true)

我需要为一个方法提供一组类型
List
,这些类型来自
apiscription
对象:

public class AddTags : IOperationFilter
{
   public void Apply(Operation operation, OperationFilterContext context)
   {
      if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methods) == true)
      {
         var test = methods.CustomAttributes
            .Where(x => x.AttributeType == typeof(CustomTag))
            .Select(x => x.NamedArguments.Where(n => n.MemberName.Equals("Tag"))
                          .Select(t => (string)t.TypedValue.Value))
            .ToList();
      }
   }
}
test
变量返回类型为
System.Linq.Enumerable.whereselectEnumerableTerator
的集合,但我需要类型为
List
的结果

在控制器中,自定义标记的应用方式如下:

[CustomTag(Tag = "TEST")]
public ActionResult MyAction()
试试这个:

var tags = methods.GetCustomAttributes(typeof(CustomTag), true)
                  .OfType<CustomTag>()
                  .Select(attribute => attribute.Tag)
                  .ToList();
var tags=methods.GetCustomAttributes(typeof(CustomTag),true)
第()类
.Select(属性=>attribute.Tag)
.ToList();

顺便说一下,
CustomTag
应该真正命名为
CustomTagAttribute
。它仍将作为
[CustomTag]

工作,谢谢,但不会编译:
'Attribute'不包含'OfType'的定义,而最佳扩展方法重载'Queryable.OfType(IQueryable)'需要'IQueryable'类型的接收器。
。我假设您正在使用System.Linq?是,我有
使用System.Linq
使用System.Reflection
那么这个错误就没有意义了。你是复制代码1:1还是键入的?您可以再次检查吗?表达式可以简化为
var tags=methods.GetCustomAttributes(true).Select(attribute=>attribute.Tag).ToList(),请参阅