我已经编写了 Linq 查询以更快速地检索记录。但是在将本地集合值传递给 Linq 查询时需要更多时间来检索:
在这里,我使用实体框架进行 LinQ 查询。我需要根据此处作为字符串集合传递的所有 platformId 获取 forumthread
目标:我们如何以更有效的方式在单个 linQ 查询中检索具有匹配 id 集合的记录?例如:慢执行查询:
public void GetThreadCollection(string[] platformid)
{
using (SupportEntity support=new SupportEntity())
{
var ThreadCollection = (from platid in platformid
from thread in support.ForumThread
from platform in support.Platforms
where platid == thread.Platformid &&
platform.PlatformId==platid
select new
{
ThreadId = thread.threadid,
Title = thread.Title,
description = thread.desc,
platformName = platform.platformName
}).ToList();
}
}
例如:然后我重写了代码以避免执行时间变慢,方法是发送单个平台 id 以使用迭代检索记录:for each: 但这也花费了前一个更少的时间。但效率不高。
前任:
public function(string[] platformId)
{
foreach(platid in platformId)
{
using (SupportEntity support = new SupportEntity())
{
var Threads = (from thread in support.ForumThread
from platform in support.Platforms
where platid == thread.Platformid &&
platform.PlatformId == platid
select new
{
ThreadId = thread.threadid,
Title = thread.Title,
description = thread.desc,
platformName = platform.platformName
}).ToList();
ThreadCollection.AddRange(threads);
}
}
}
您能否建议,如何在单个查询中更有效地获取写入单个查询以检索记录?