0

如果在其他地方已经回答了这个问题,我正在学习 EF,所以很抱歉。我找不到解决方案。我正在使用两个跟踪查询,因为我不能使用 Include,因为它不支持条件。所以我的代码如下:

    List<int> CategoryIDs = _categoryIDs.Split(',').Select(t => int.Parse(t)).ToList();

我的模型:

 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; } 
}

然后我有我的视图模型:

public class HomeIndexData
{

    public IEnumerable<Genre> Genres { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<SubCategory> SubCategories { get; set; }         

}

然后我试图将视图模型返回到我的索引:

     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);      

        }

它返回结果,但我也想过滤子类别。如何放置 WHERE 条件而不是 .Include(i => i.SubCategories)。

请注意我不想返回匿名类型,这就是为什么我是两个跟踪查询。

提前致谢。

4

1 回答 1

0

Right, I think I know what you want to do here:

// so filter out categories
var categories = db.Categories.Where(c => CategoryIDs.Contains(c.CategoryID)); 

// so filter out subcategories - I'm not sure what kind of filtering you want on subcategories
var subcategories = db.Subcategories.Where(s => CategoryIDs.Contains(s.SubCategoryID)); 
// so alternatively just do 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
    {
        // very good question? what are you going to do with subcategories that
        //match your criteria, but its category does not meet category filter
    }

in your model you need to 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 then you end up only with categories that match your filter and contains only subcategories that match the filter. All strongly typed and tracked.

于 2013-04-06T11:09:01.757 回答