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

bin*_*nks 16 c# linq entity-framework

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

var rootCategoryItem = DatabaseContext.Categories
                            .Include("SubCategories")
                            .OrderBy(c => c.CategoryOrder)
                            .Single(c => c.CategoryId == 1);
Run Code Online (Sandbox Code Playgroud)

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

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

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

        var subCategoriesToFilter = rootCategoryItem.SubCategories.ToList();

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

第二种选择是在我看来:

<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>
Run Code Online (Sandbox Code Playgroud)

在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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Ser*_*rvy 20

虽然您无法过滤包含的集合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);
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个解决方案,但是,我在这一行得到一个错误:SubCategories = c.SubCategories.Where(sub =>!sub.Deleted).OrderBy(sub => sub.CategoryOrder),不能隐式转换类型'System. Linq.IOrderedEnumerable <Secure_Password_Repository.Models.Category>'到'System.Collections.Generic.ICollection <Secure_Password_Repository.Models.Category>'.存在显式转换(您是否错过了演员?) (2认同)
  • 好的,但是使用.ToList()应该从ICollection继承...但我仍然得到新的错误:实体或复杂类型'.Category'不能在LINQ to Entities查询中构造. (2认同)