C# 在任意at=IsAvailable=下获得一条红线!b、 lookHistories.Any(h=>;h.ReturnDate==null)
你好,我有这个错误 “ICollection”不包含“Any”的定义,并且找不到接受“ICollection”类型的第一个参数的可访问扩展方法“Any”(是否缺少using指令或程序集引用?) 在任意at=IsAvailable=下获得一条红线!b、 lookHistories.Any(h=>h.ReturnDate==null) 我正在学习如何使用Asp.NETMVC创建图书馆管理系统的教程 谢谢C# 在任意at=IsAvailable=下获得一条红线!b、 lookHistories.Any(h=>;h.ReturnDate==null),c#,asp.net-mvc,C#,Asp.net Mvc,你好,我有这个错误 “ICollection”不包含“Any”的定义,并且找不到接受“ICollection”类型的第一个参数的可访问扩展方法“Any”(是否缺少using指令或程序集引用?) 在任意at=IsAvailable=下获得一条红线!b、 lookHistories.Any(h=>h.ReturnDate==null) 我正在学习如何使用Asp.NETMVC创建图书馆管理系统的教程 谢谢 AndreasAny是IEnumerable的一种扩展方法,ICollection不实现(但IC
Andreas
Any
是IEnumerable
的一种扩展方法,ICollection
不实现(但ICollection
实现)。如果你能得到一个i集合
,你就可以使用它;否则,您可以编写自己的高效Any
(工作方式与.NET版本相同,带有性能调整.NET Framework没有,但.NET Core有):
注意:下面的代码段使用较旧的C语言版本,因为不知道基线是什么
公共类扩展
{
公共静态bool Any(此IEnumerable源)
{
if(source==null)抛出ArgumentNullException(nameof(source));
//性能调整,以消除枚举器的分配,如果我们有一个计数。
如果(源是ICollection)返回((ICollection)源)。计数>0;
IEnumerator e=source.GetEnumerator();
bool any=e.MoveNext();
//如有必要,请确保正确处理枚举数。
//IEnumerator实现IDisposable,但IEnumerator本身的实现可能会。
如果(e是可识别的)((可识别的)e.Dispose();
退回任何文件;
}
}
我将BookController中的public ICollection BorrowHistories{get;set;}更改为public ICollection BorrowHistories{get;set;},这实现了您建议的技巧。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LibraryManagement.Models;
using Microsoft.Ajax.Utilities;
namespace LibraryManagement.Controllers
{
public class BooksController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Books
public ActionResult Index()
{
var books = db.Books.Include(h => h.BorrowHistories)
.Select(b => new BookViewModel
{
BookId = b.BookId,
Author = b.Author,
Publisher = b.Publisher,
SerialNumber = b.SerialNumber,
Title = b.Title,
IsAvailable = !b.BorrowHistories.Any(h => h.ReturnDate == null)
}).ToList();
return View(books);
}