asp.net mvc 3 - Entity Framework 4.1 Two Tracked Queries -
i learning ef apology if has been answered else. not find solution this. using 2 tracked queries because cannot use include not support condition. code follow:
list<int> categoryids = _categoryids.split(',').select(t => int.parse(t)).tolist();
my models:
public class genre { public int genreid { get; set; } public string name { get; set; } public string iconurl{ get; set; } public string description { get; set; } public int displaysequence { get; set; } public ilist<category> categories { get; set; } public class category { public int categoryid { get; set; } public int genreid { get; set; } public string categoryname { get; set; } public virtual genre genre { get; set; } } public class subcategory { public int subcategoryid { get; set; } public int categoryid { get; set; } public string subcategoryname { get; set; } public virtual category category { get; set; } }
then have view model:
public class homeindexdata { public ienumerable<genre> genres { get; set; } public ienumerable<category> categories { get; set; } public ienumerable<subcategory> subcategories { get; set; } }
then trying return viewmodel index:
public actionresult index() { var genres = db.genres.tolist().orderby(g => g.displaysequence); var categories = db.categories.include(i => i.subcategories) .where(i => categoryids.contains(i.categoryid)); foreach (var category in categories) { }; homeindexdata viewmodel = new homeindexdata { genres = genres }; return view(viewmodel); }
it returns result wanted filter subcategories well. how put condition instead of .include(i => i.subcategories).
please note dnt want return anonymous type, why 2 tracked queries.
thanks in advance.
right, think know want here:
// filter out categories var categories = db.categories.where(c => categoryids.contains(c.categoryid)); // filter out subcategories - i'm not sure kind of filtering want on subcategories var subcategories = db.subcategories.where(s => categoryids.contains(s.subcategoryid)); // alternatively this: var subcategories = db.subcategories.where(s => s.doyourfilteringforsubcategories); foreach (var subcategory in subcategories) var cat = categories.singleordefault(c => c.categoryid == subcategory.categoryid) if (cat != null) { cat.subcategories.add(subcategory); } else { // question? going subcategories //match criteria, category not meet category filter }
in model need add:
public class category { public int categoryid { get; set; } public int genreid { get; set; } public string categoryname { get; set; } public virtual genre genre { get; set; } **public virutal list<subcategory> subcategories{ get; set; }** }
so end categories match filter , contains subcategories match filter. typed , tracked.
Comments
Post a Comment