c# - How to query with a join with linq to sql -
i'm trying query gifts given term categories table. entity framework created bridge table connect "gift" "giftcategroies". query have yielded no results.
from dbcontext:
public dbset<gift> gifts { get; set; } public dbset<giftcategory> categories { get; set; }
the 2 entities created:
public class gift { public int id { get; set; } public string name { get; set; } public icollection<giftcategory> categories { get; set; } public int rating { get; set; } } public class giftcategory { public int id { get; set; } public string name { get; set; } public string description { get; set; } public icollection<gift> gifts { get; set; } }
here query try , fetch gifts given giftcategory term. no results returned. i'm not sure if need join type of query.
var model = gifts in db.gifts join giftcategory in db.categories on gifts.id equals giftcategory.id giftcategory.name.contains(searchterm) select gifts;
you should use navigation properties instead of joins:
var gifts = (from c in db.categories g in c.gifts c.name.contains(searchterm) select g).distinct().tolist();
Comments
Post a Comment