我有下面的代码片段,它在"i.tPersons.Any"处生成错误,如下所示:
"WhatWorks.Models.tPerson"不包含"Any"的定义,并且找不到接受类型为"WhatWorks.Models.tPerson"的第一个参数的扩展方法"Any"(您是否缺少使用指令或程序集引用?)
"任何"是System.Data.Entity的一种方法,所以我希望它被拾取。我错过了什么?
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhatWorks.Models;
namespace WhatWorks.Controllers
{
public class InterventionController : Controller
{
private WhatWorksEntities db = new WhatWorksEntities();
//
// GET: /Intervention/
// where parameter list only includes id
public ActionResult Index(int id)
{
var model =
(
from i in db.tInterventions
where (i.householdID == id && !(i.tPersons.Any(t => i.householdID == id)))
select i
);
Any() 是一个 Linq 扩展方法,可以应用于 IEnumerables 和 IQueryables。
看起来您正在执行.Any() 在 tPerson 集合上,但在 Any 的谓词中,您使用的是"i",在您的情况下是 tInterventions 的实例。
因此,要么将 i.homeholdID 中的 i 替换为 t.householdID,要么直接在 tIntervensions 上执行 Any。