I have the code snippet below which generates an error at “i.tPersons.Any” as:
‘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’ 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() is a Linq extension method that can be applied to IEnumerables and IQueryables.
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.
So, either replace the i in i.householdID with t.householdID or execute Any on tIntervensions directly.