System.Data.Entity不能按照MVC实体框架的预期工作 [英] System.Data.Entity not working as expected for MVC Entity Framework
问题描述
我有下面的代码片段,在i.tPersons.Any中生成错误:
I have the code snippet below which generates an error at "i.tPersons.Any" as:
'WhatWorks.Models.tPerson'不包含定义对于'Any'和没有扩展方法'Any'接受可以找到类型WhatWorks.Models.tPerson的第一个参数(您是否缺少using指令或程序集引用?)
'WhatWorks.Models.tPerson' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'WhatWorks.Models.tPerson' could be found (are you missing a using directive or an assembly reference?)
'Any'是System.Data.Entity的一个方法,所以我希望这个被拾起。我缺少什么?
'Any' is a method of System.Data.Entity so I would expect this to be picked up. What am I missing?
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() is a Linq extension method that can be applied to IEnumerables and IQueryables.
看起来你正在执行.Any()在tPersons集合,但在谓词为任何你正在使用'我'你的案例是tInterventions的一个实例。
It looks like you are executing .Any() on the tPersons collection but in the Predicate for Any you are using 'i' which is in your case an instance of tInterventions.
所以,要么将i.householdID中的i替换为t.householdID,要么直接在tIntervension上执行Any。
So, either replace the i in i.householdID with t.householdID or execute Any on tIntervensions directly.
这篇关于System.Data.Entity不能按照MVC实体框架的预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!