I am trying to filter displayed documents in a winforms app using a checked list box, so that when 2 tags are selected only documents containing those tags would show up and be further winnowed down when a third tag was selected. I am using Entity Framework. Here is what I have but I think it might not be efficient. I don’t like that I have to query the db so often. Any thoughts?
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);
}
}
}
}
Now I am trying to display the docs based on the ids but… here’s the new code
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);
You can basically just do a contains on tags:
For the JOIN: