【问题标题】:Filtering a list of documents through selected tags通过选定的标签过滤文档列表
【发布时间】:2012-08-04 12:52:46
【问题描述】:

我正在尝试使用选中的列表框过滤 winforms 应用程序中显示的文档,以便在选择 2 个标签时,只有包含这些标签的文档会显示出来,并在选择第三个标签时进一步筛选。我正在使用实体框架。这是我所拥有的,但我认为它可能效率不高。我不喜欢我必须经常查询数据库。有什么想法吗?

List<int> docIds = null;
        if (tags != null)
        {
            docIds.AddRange(from di in frmFocus._context.AllocateDocumentTags
                            where di.tagId == tags[0]
                            select di.documentId);
            for (int i = 1; i < tags.Length; i++)
            {
                List<int> docList = (from dId in frmFocus._context.AllocateDocumentTags
                                     where dId.tagId == tags[i]
                                     select dId.documentId).ToList();
                foreach (int n in docIds)
                {
                    if (!docList.Contains(n))
                    {
                        docIds.Remove(n);
                    }
                }
            }

        }

现在我正在尝试根据 ID 显示文档,但是……这是新代码

docIds = (from di in frmFocus._context.AllocateDocumentTags.Distinct()
                                    where tags.Contains(di.tagId)
                                    select di.documentId).ToList();
                tagManagment.fillUsed(docIds);
            }
            ObjectSet<Documents> _docs = (from d in frmFocus._context.Documents
                     where docIds.Contains(d.id)
                     select d);

【问题讨论】:

    标签: c# winforms entity-framework-4 filtering


    【解决方案1】:

    你基本上可以在标签上做一个包含:

    List<int> docIds = (from di in frmFocus._context.AllocateDocumentTags
                        where tags.Contains(di.tagId)
                        select di.documentId);
    

    对于 JOIN:

    ObjectSet<Documents> _docs = (from doc in docIds
                                  join d in frmFocus._context.Documents.ToList()  on doc equals d.id
                                  select d);
    

    【讨论】:

    • 不会是“where tags.Contains(di.tagId)”吗?
    • @The_Cthulhu_Kid 是的,对不起。更新了!
    • 不,那太好了,我只是想确定一下。非常感谢。这更简洁!
    • @The_Cthulhu_Kid 酷,很高兴我能帮上忙。
    • 很抱歉再次打扰您,但我现在无法获取这些 ID 并取回文件。我会在上面发布代码,如果您没有时间,请不要担心。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2020-10-25
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多