【发布时间】:2019-08-09 08:37:26
【问题描述】:
美好的一天,我正在编写一个使用即时加载的代码,以使用Include 方法从Assigned 加载相关数据。我正在尝试获取IsAvailable 的值,以便从Assigned 访问数据并检查是否存在ReturnDate 值等于null 的记录。我不断收到错误提示
Icollection 不包含任何的定义
public ActionResult Index()
{
var item = db.Item.Include(h => h.Assigned).Select(b => new ItemViewModel
{
ItemID = b.ItemID,
ItemName = b.ItemName,
Category = b.Category,
Description = b.Description,
Model = b.Model,
IsAvailable = !b.Assigned.Any(h => h.ReturnDate == null)
}).ToList();
return View(item);
}
项目视图模型
public class ItemViewModel
{
public int ItemID { get; set;}
[Display(Name = "Item")]
public string ItemName { get; set; }
[Required(ErrorMessage = "Category is required")]
public string Category { get; set; }
public string Model { get; set; }
public string Description { get; set; }
public bool IsAvailable { get; set; }
}
物品类别
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Collections;
namespace Inventoryproto3.Models
{
public class Item
{
public int ItemID { get; set; }
[Required(ErrorMessage ="Please enter a title"),Display(Name ="Item")]
public string ItemName { get; set;}
[Required(ErrorMessage ="Category is required")]
public string Category { get; set;}
public string Model { get; set;}
public string Description{get; set;}
public ICollection Assigned { get; set; }
}
}
“System.Collections.Generic.ICollection”不包含“Any”的定义,并且找不到接受“System.Collections.Generic.ICollection”类型的第一个参数的可访问扩展方法“Any”(你是缺少 using 指令或程序集引用?
【问题讨论】:
-
发布你的
Item实体类。 -
发布 exact 错误消息。你是
using System.Linq;吗? -
您是否包含 System.Linq?
b.Assigned的类型是什么? -
我确实包含了 system.linq ,B.assigned 是一个 Icollection 属性
-
您需要将您的属性定义为 generic
ICollection才能使用 LINQ 功能。例如:ICollection<Assigned>.
标签: asp.net-mvc entity-framework linq model-view-controller