c# - LINQ select statement with many to many -
i have relationship, project categories. relation many many have 3 tables: project / project_has_category / categories.
i need select projects has relation category (by id)
project class
public class project { public int projectid { get; set; } public virtual icollection<category> categories { get; set; } }
category class
public class category { public int categoryid { get; set; } public string categoryname { get; set; } public virtual icollection<project> projects { get; set; } }
i have tried following:
[httppost] public actionresult projects(string catid, string strsearch) { var cats = adapter.categoryrepository.get(); var projects = adapter.projectrepository.get().where(x => x.categories.contains(catid)); /*also*/ var projects = adapter.projectrepository.get().where(x => cats.contains(catid)); return view(projects); }
but gives error:
the best overloaded method match 'system.collections.generic.icollection.contains(libmodels.category)' has invalid arguments c:\users\thomas\desktop\freelauncher1005\freelauncher\controllers\projectcontroller.cs
what doing wrong?
categories list of category
objects, can't search integer id contains
method (check signature of method - requires category
object search for):
var projects = adapter.projectrepository.get() .where(x => x.categories.contains(catid)) // error
use any
check if there category
object id equal value:
var projects = adapter.projectrepository.get() .where(x => x.categories.any(c => c.categoryid == catid))
Comments
Post a Comment