【问题标题】:How to filter results in Entity Framework with multiple select box如何使用多个选择框过滤实体框架中的结果
【发布时间】:2016-07-14 06:51:12
【问题描述】:

我有三个用于博客文章的表格:

  • post_table 用于 post_id、post_title 等帖子属性。
  • category_table 包括两列,例如 category_id 和 category_name
  • 和post_category_table是post_table和category_table之间关系的接口表,它有两个外键列,例如post_id和category_id。

请注意,每个帖子可以有一个或多个类别。

multiple select box (image)

现在我有一个用于类别的多选框(如附件图片),用户可以选择一个或多个类别。当用户从选择框中选择一个或多个类别时,结果应仅限于选定的类别。

如何使用 Entity Framework 为这个问题编写“Where”条件?

我的代码是:

var query = context.post_Table
    .Select(postinfo => new {
        post_title = postinfo.post_title,
        post_date = postinfo.post_date,
        post_id=postinfo.post_id,          
        post_category = postinfo.post_category_table.Select(cc => new {
            cc0=cc.category_id,
            cc1 = cc.category_table.category_name,
        }),

    }).Where(???)

【问题讨论】:

    标签: c# sql-server asp.net-mvc entity-framework


    【解决方案1】:

    您应该能够从多选框中获取类别 id 的集合(所选类别的 id)。

    我们称之为selectedCategoryIds,假设它是IEnumerable<int>。

    要根据所选类别过滤结果,您可以这样做:

    var query = context.post_Table
        .Select(postinfo => new 
        {
            post_title = postinfo.post_title,
            post_date = postinfo.post_date,
            post_id=postinfo.post_id,          
            post_category = postinfo.post_category_table.Select(cc => new 
            {
                cc0 = cc.category_id,
                cc1 = cc.category_table.category_name,
            })
        })
        .Where(info => info.post_category.Any(c => selectedCategoryIds.Contains(c.cc0)));
    

    编辑

    如果您有一些条件逻辑,您可以相应地拆分和组合查询:

    var query = context.post_Table
        .Select(postinfo => new 
        {
            post_title = postinfo.post_title,
            post_date = postinfo.post_date,
            post_id=postinfo.post_id,          
            post_category = postinfo.post_category_table.Select(cc => new 
            {
                cc0 = cc.category_id,
                cc1 = cc.category_table.category_name,
            })
        });
    
    if(selectedCategoryIds != null && selectedCategoryIds.Any())
    {
        query = query.Where(info => info.post_category.Any(c => selectedCategoryIds.Contains(c.cc0)));
    }
    

    但是,如果您的条件逻辑比上面的简单示例更复杂,请查看my answer to a previous question,了解您应该使用谓词构建器的原因。

    【讨论】:

    • 感谢您的回复。但它返回了info.post_category.cc0 的错误。错误信息是:IEnumerable<<anonymous type: int cc0, string cc1>>' does not contain a definition for 'cc0' and no extension method 'cc0' accepting a first argument of type 'IEnumerable<<anonymous type: int cc0, string cc1>>' could be found (are you missing a using directive or an assembly reference?)
    • @msitworld,抱歉;我没有注意到post_category 是一个集合。我更正了答案。
    • 非常感谢。有效。再次感谢。 :)))
    • 如果selectedCategoryIds 为null,则应返回所有类别的结果,但是当selectedCategoryIds 为null 时会出现这样的异常:Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable'1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only entity types, enumeration types or primitive types are supported in this context. 可以帮助我吗?
    • @msitworld,我已经编辑了答案以提供更多信息。
    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2013-11-01
    • 2019-11-10
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多