这似乎是一个直截了当的问题。我开始怀疑剃刀中的一个错误。
if (Model.athleteImages .Any()){
//some code
}
我已将上述内容替换为以下内容:
if (Model.athleteImages.Count > 0)
{
var i = 0;
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
while (i + 1 < Model.athleteImages.Count())
{
i++;
<li data-target="#carousel-example-generic" data-slide-to="@i"></li>
}
}
这总是抛出&#34;序列不包含任何元素&#34;对于没有元素的集合,例如Count()= 0.
Any()应该测试一个Sequence是否包含元素。这就是它的全部目的。
我也试过
if (Model.athleteImages.FirstOrDefault().[fieldName] != null){
//some code
}
同样的结果。
这里是来自控制器的一些相关代码,用于那些想知道图像集合是什么
的人var adImages = from i in db.athleteImages
where thisAd.albumId == i.albumId
where i.deleted == false
select i;
viewModel.athleteImages = athleteImages.ToList();
以下是View Model Class
public class ListingViewModel
{
public site site { get; set; }
public userAd userAd { get; set; }
public List<athleteImage> athleteImages { get; set; }
public string categoryName { get; set; }
public int categoryId { get; set; }
public string subcategoryName { get; set; }
public int subcategoryId { get; set; }
}
堆栈跟踪
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.First(IEnumerable`1 source) +269
ASP._Page_Views_userAds_Listing_cshtml.Execute() in c:\Users\Bill\Documents\Visual Studio 2013\Projects\CuriousMarketplaces\CuriousMarketplaces\Views\userAds\Listing.cshtml:32
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
System.Web.WebPages.StartPage.RunPage() +17
System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
答案 0 :(得分:0)
如果您真正关心的是集合中是否有任何图像,那么这样的事情可能会更有意义:
if (Model.Images.Count > 0) {
}
答案 1 :(得分:0)
如果源为null,Any()将抛出异常,这是记录的行为以及它应该如何工作:http://msdn.microsoft.com/en-us/library/vstudio/bb534972%28v=vs.100%29.aspx
Dave的解决方案非常好但我会添加一个空检查,如果对象为null,Count也会抛出异常。
if(Model.Images != null && Model.Images.Count > 0) {
}
答案 2 :(得分:0)
即使调试器指示我的if块的关闭块发生了错误,但事实并非如此。
在同一视图的后面的代码块中,我有:
<div class="item active" id="0">
<div style="width:400px;height:300px;text-align:center;background-color:black;">
<img src="/Content/Images/Deals/1/@Model.athleteImages.First().fullImage" style="height:300px;" alt="...">
<div class="carousel-caption">
some caption
</div>
</div>
</div>
要解决问题,我需要再次测试。这样:
if (Model.athleteImages.Count > 0)
{
<div class="item active" id="0">
<div style="width:400px;height:300px;text-align:center;background-color:black;">
<img src="/Content/Images/Deals/1/@Model.athleteImages.First().fullImage" style="height:300px;" alt="...">
<div class="carousel-caption">
some caption
</div>
</div>
</div>
}
我的大脑是什么炸薯条,错误信息表明第32行出现问题。实际上它出现在第37行。