繁体   English   中英

过滤包括LINQ和实体框架中的项目

[英]Filtering include items in LINQ and Entity Framework

我目前在我的应用程序中有这个LINQ / EF代码:

var rootCategoryItem = DatabaseContext.Categories
                            .Include("SubCategories")
                            .OrderBy(c => c.CategoryOrder)
                            .Single(c => c.CategoryId == 1);

我知道在EF中你不能过滤包含的项目,我可以编写一些LINQ代码来过滤掉不需要的子类别......但是LINQ代码被转换为一个非常不优化的可怕的SQL。 我也可以编写一个执行此操作的存储过程(并编写比LINQ更好的查询),但我真的想使用纯EF。

所以我留下了2个选项(除非有人可以看到其他选项)。

第一个是遍历子类别,删除不需要的子类别:

        var subCategoriesToFilter = rootCategoryItem.SubCategories.ToList();

        for (int i = 0; i < subCategoriesToFilter.Count; i++)
        {
            if (subCategoriesToFilter[i].Deleted)
                rootCategoryItem.SubCategories.Remove(subCategoriesToFilter[i]);
        }

第二种选择是在我看来:

<ul class="treeview ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion ui-widget ui-sortable ui-accordion-content-active">
@foreach (var categoryitem in Model.SubCategories.OrderBy(c => c.CategoryOrder))
{

    @if(!Model.Deleted)
    { 
        <li class="treelistitem" id="@Model.CategoryId">
            <div class="ui-accordion-header ui-state-default ui-corner-all ui-accordion-icons ui-sortable-handle first">
            <span class="clickable">
                <span class="ui-accordion-header-icon ui-icon treeviewicon treeviewplus"></span>
                <i class="glyphicon glyphicon-folder-open rightfolderpadding"></i><span class="categoryname">@Model.CategoryName</span>
            </span>
            </div>
           </li>
    }
}   
</ul>

在2中,哪一个是最好的选择? 或者我还有另一种选择吗?

解决方案

好的,Servy的非常正确,我不得不修改他的答案以使其工作:

        var rootCategoryItem = DatabaseContext.Categories
            .OrderBy(c => c.CategoryId)
            .ToList().Select(c => new Category()
            {
                SubCategories = c.SubCategories.Where(sub => !sub.Deleted).ToList(),    //make sure only undeleted subcategories are returned
                CategoryId = c.CategoryId,
                CategoryName = c.CategoryName,
                Category_ParentID = c.Category_ParentID,
                CategoryOrder = c.CategoryOrder,
                Parent_Category = c.Parent_Category,
                Deleted = c.Deleted
            }).Single(c => c.CategoryId == 1);

我试图让Servy的解决方案工作有几个错误:

无法在LINQ to Entities查询中构造实体或复杂类型“.Category”

无法将类型隐式转换为System.Collections.Generic.ICollection。 存在显式转换(您是否错过了演员?)

通过在Select()方法之前添加.ToList()来解决这个问题。

虽然您无法过滤通过Include的集合,但您可以使用Select并将该集合投影到过滤集合中。

var rootCategoryItem = DatabaseContext.Categories
    .OrderBy(c => c.CategoryOrder)
    .Select(c => new Category()
    {
        SubCategories = c.SubCategories.Where(sub => !sub.Deleted)
            .OrderBy(sub => sub.CategoryOrder),
        c.CategoryId,
        c.CategoryName,
        //include any other fields needed here
    })
    .Single(c => c.CategoryId == 1);

我通过这种方式看起来更清洁,更短。 不确定数据库影响

 var rootCategoryItem = DatabaseContext.Categories.SingleOrDefault();
 if (rootCategoryItem == null) return null;
 {
   rootCategoryItem.Items = rootCategoryItem ?.Items.Where(x => !x.IsDeleted).ToList();
   return rootCategoryItem;
  }

您在此处的担忧是演示问题(仅显示未删除的类别)。 这表明方法2是您的最佳选择。

但是,我怀疑您需要经常在系统中使用未删除的类别。 这表明您应该有一个函数可以一致地返回未删除的类别,供您在任何需要的地方使用。

因此,我建议使用方法1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM