简体   繁体   English

我不能在.ascx上使用IEnumerable()吗?

[英]Can't I use IEnumerable() on .ascx?

This is my code : 这是我的代码:

protected IEnumerable<MyObject> CategoriesHotelsFiltrati;
CategoriesHotelsFiltrati.Union(CategoriesHotels.Where(o => o.Comune != null && CategoriesLocalitaSelezionate.Contains(o.Comune.UniqueID)));

now, on .asxc, if I try to do : 现在,在.asxc上,如果我尝试这样做:

<% 
    if (m_oHotelsFiltrati == null || m_oHotelsFiltrati.Count()==0)
    {
        Response.Write("hello");
    }
%>  

seems that it doesnt find .Count() method. 似乎它没有找到.Count()方法。 It says somethings about "using" or "assembly". 它说有关“使用”或“组装”的事情。 Strange, with IList<> this works perfectly...why? 奇怪,与IList<>这完美地工作......为什么?

You need to add the following line to your *.ascx file: 您需要*.ascx下行添加到*.ascx文件中:

<%@ Import namespace="System.Linq" %>

See this link for more details. 有关详细信息,请参阅此链接

.Count() is an extension method, it's not actually a method of IEnumerable . .Count()是一个扩展方法,它实际上不是IEnumerable的方法。 You need to have a using for System.Linq for the compiler to find the method. 您需要using System.Linq来使编译器找到该方法。 (As per comment by Anthony Pegram, you would use the import command for a markup file.) (根据Anthony Pegram的评论,您可以使用import命令获取标记文件。)

It works fine with an IList because list actually has a property Count ; 它适用于IList,因为列表实际上有一个属性Count ; it doesn't rely on the extension method. 它不依赖于扩展方法。

With IList<> you're probably calling the Count property (without parentheses). 使用IList<>您可能正在调用Count属性(没有括号)。 You can call Count() as a static method: 您可以将Count()作为静态方法调用:

<%  
    if (m_oHotelsFiltrati == null || Enumerable.Count(m_oHotelsFiltrati)==0) 
    { 
        Response.Write("hello"); 
    } 
%>   

I'm not sure how to get extension method resolution in the .ascx file. 我不知道如何在.ascx文件中获得扩展方法解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在.ascx中使用“ var”变量? - Can't use “var” variables in the .ascx? 如何在.ascx中使用“using”指令? - How can I use the “using” directive in a .ascx? 为什么我不能使用以下IEnumerable <string> ? - Why can't I use the following IEnumerable<string>? 试图了解我的 IEnumerable 以及为什么我不能使用.ToList() - Trying to understand my IEnumerable and why I can't use .ToList() 如何在任务中使用<actionresult<ienumerable<t> &gt;&gt; </actionresult<ienumerable<t> - How can I use in Task<ActionResult<IEnumerable<T>>> 我可以用IEnumerable编写此lambda吗 <T> 而不是IEnumerable <string> ? - Can I write this lambda with IEnumerable<T> rather than IEnumerable<string>? 我无法让我的.ascx显示新内容 - I can't get my .ascx to display new content 为什么我不能在 a.ascx 中添加这个“自定义控件”? - Why I can't add this "custom control" in a .ascx? 当我的类实现IEnumerable时,为什么不能使用LINQ to Objects <T> 多次? - Why can't I use LINQ to Objects when my class implements IEnumerable<T> multiple times? 我可以(或应该)使用 IAsyncEnumerable<T> 而不是任务<ActionResult<IEnumerable<T> &gt;&gt; 在 Web API 控制器中 - Can (or should) I use IAsyncEnumerable<T> instead of Task<ActionResult<IEnumerable<T>>> in a Web API Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM