无法从哈希集中获取包含(项)的Linq查询中的位置

时间:2017-05-06 13:03:53

标签: c# sql-server linq

更新:所以看起来我需要这个:linq to sql startwith performance indexed columns但不清楚我需要把代码放在哪些文件中。

我有以下基本查询:

resultsPeople = db.People
.Where(c => c.StudyDesign.Contains(item) && initialWithFiltersPeopleHashset.Contains(c.ID))
.Select(c => new { c.ID });

现在,如果我从一个直接的查询中获取结果:

var resultsPeople = db.People
.Where(c => c.StudyDesign.Contains("Trial") && initialWithFiltersPeopleHashset.Contains(c.ID))
.Select(c => new { c.ID });

我得到了正确的结果。

但是,如果我这样做:

public ActionResult Index(string SearchString, string[] filters)
{
    string[] searchTermArray = SearchString.Split(' ');
    var peopleList = new HashSet<string> { };
    foreach (var filter in filters) {
        peopleList.Add(filter);
    }
    var initialWithFiltersCohortHashset = new List<string>();
    var filtersNotNullCohorts = db.People
    .Join(db.OArea, c => c.ID, oa => oa.PersonID, (c, oa) => new { c = c, oa = oa })
    .Join(db.OFArea, oa => oa.oa.PersonID, ofa => ofa.PersonID, (oa, ofa) => new { oa = oa, ofa = ofa })
    .Join(db.Criteria, ofa => ofa.ofa.PersonID, iec => iec.PersonID, (ofa, iec) => new { ofa = ofa, iec = iec })
    .Join(db.OC, iec => iec.iec.PersonID, o => o.PersonID, (iec, o) => new { iec = iec, o = o })
    .Where(o => searchTermArray.Any(x => o.o.PO.Contains(x)) || searchTermArray.Any(x => o.iec.ofa.oa.oa.OT.Contains(x)) || o.iec.ofa.oa.oa.Category.Contains(SearchString) || searchTermArray.Any(x => o.iec.ofa.ofa.FArea.Contains(x)) || searchTermArray.Any(x => o.iec.iec.Criteria.Contains(x)))
    .Select(c => new { c.iec.ofa.oa.c.ID }).Distinct();

    foreach (var person in filtersNotNullCohorts)
    {
        initialWithFiltersCohortHashset.Add(person.ID.ToString());
        if (!noFilterCohortHashset.Contains(person.ID.ToString()))
        {
            noFilterCohortHashset.Add(person.ID.ToString());
        }
    }

        foreach (var item in peopleList)
        {
            var resultsPeople = db.People
            .Where(c => c.StudyDesign.Contains(item) && initialWithFiltersPeopleHashset.Contains(c.ID))
            .Select(c => new { c.ID });

            foreach (var person in resultsPeople)
            {

                studyDesignTempCohortHashset.Add(person.ID.ToString());
            }
        }
}

它不会返回任何结果。为什么这是个问题?如果我打印出查询,它看起来像:

SELECT 1 AS [C1], [Extent1].[ID] AS [ID] FROM [dbo].[People] AS [Extent1] WHERE ([Extent1].[StudyDesign] LIKE @p__linq__0 ESCAPE N'~') AND ([Extent1].[ID] IN (N'01301', N'02401', N'01312', N'01402', N'03201', N'00402', N'01303', N'01310', N'01304', N'02402', N'00201', N'01801', N'01101', N'02301', N'01307', N'03501', N'01001', N'02701', N'01306', N'01309', N'00401', N'01311', N'00203', N'03502'))

我还是C#和linq的新手,所以任何建议都表示赞赏。

0 个答案:

没有答案