C# Can';从hashset获取Linq查询中where的包含项
更新:所以看起来我需要这个:但是不清楚我需要把代码放在什么文件中 我有以下基本查询:C# Can';从hashset获取Linq查询中where的包含项,c#,sql-server,linq,C#,Sql Server,Linq,更新:所以看起来我需要这个:但是不清楚我需要把代码放在什么文件中 我有以下基本查询: resultsPeople = db.People .Where(c => c.StudyDesign.Contains(item) && initialWithFiltersPeopleHashset.Contains(c.ID)) .Select(c => new { c.ID }); 现在,如果我直接从查询中获取结果,如: var resultsPeople = db.Peo
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());
}
}
}
我对C#和linq还不熟悉,所以非常感谢您的建议。您的
linq
查询尚未执行。添加.ToList()
或.ToArray()
以获得在foreach()中运行查询的延迟执行?是的,尝试一下。两者都不起作用。您的问题不清楚您正在对完全不同的内容进行迭代,请编写完整的代码!
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'))