我需要将复杂实体的过滤从Linq到SQL移到存储过程,因为我需要将数据加载到内存中,以便使用ToList()应用某些过滤器。 Linq查询导致某些搜索超时。当我切换到存储过程时,将因此更改功能:
private IQueryable<ProposalLogVM> ApplyProposalSearch(IndexSearchVM search)
{
SqlParameter YearCode;
SqlParameter ProposalStatus;
SqlParameter Company;
SqlParameter SearchString;
if (!String.IsNullOrWhiteSpace(search.SearchString) && search.Year == null)
{
search.Year = DateTime.Now.Year;
}
if (!String.IsNullOrWhiteSpace(search.Year.ToString()))
{
YearCode = new SqlParameter("@Year", search.Year.ToString());
}
else
{
YearCode = new SqlParameter("@Year", DBNull.Value);
}
if (!String.IsNullOrWhiteSpace(search.ProposalStatus))
{
ProposalStatus = new SqlParameter("@ProposalStatus", search.ProposalStatus);
}
else
{
ProposalStatus = new SqlParameter("@ProposalStatus", DBNull.Value);
}
if (!String.IsNullOrWhiteSpace(search.CompanyID.ToString()))
{
Company = new SqlParameter("@CompanyID", search.CompanyID.ToString());
}
else
{
Company = new SqlParameter("@CompanyID", DBNull.Value);
}
if (!String.IsNullOrWhiteSpace(search.SearchString))
{
SearchString = new SqlParameter("@SearchString", search.SearchString);
}
else
{
SearchString = new SqlParameter("@SearchString", DBNull.Value);
}
var proposals = _dbpm.Database.SqlQuery<ProposalLogVM>("EXEC spSearchProposals @Year, @ProposalStatus, @CompanyID, @SearchString", YearCode, ProposalStatus, Company, SearchString).AsQueryable();
switch (search.SortOption)
{
case "ProposalNumber":
proposals = proposals.OrderBy(p => p.ProposalNumber);
break;
case "ProjectName":
proposals = proposals.OrderBy(p => p.ProjectName);
break;
case "ProjectNameD":
proposals = proposals.OrderByDescending(p => p.ProjectName);
break;
case "BidDate":
proposals = proposals.OrderBy(p => p.BidDate);
break;
case "BidDateD":
proposals = proposals.OrderByDescending(p => p.BidDate);
break;
case "Owner":
proposals = proposals.OrderBy(p => p.Owner);
break;
case "OwnerD":
proposals = proposals.OrderByDescending(p => p.Owner);
break;
default:
proposals = proposals.OrderByDescending(p => p.ProposalNumber);
break;
}
return proposals;
}
现在,我得到“另一个SQLParameterCollection已经包含了SqlParameter”。执行该行时
// get the raw proposal list, but don't execute or copy to memory
IQueryable<ProposalLogVM> rawProposals = ApplyProposalSearch(viewModel.Search);
// Allow ToPagedList() to do the hard work
viewModel.Proposals = rawProposals.ToPagedList((int)viewModel.Search.Page, (int)viewModel.Search.Lines);
如果我只执行该行:
var proposals = _dbpm.Database.SqlQuery<ProposalLogVM>("spSearchProposals @Year, @ProposalStatus, @CompanyID, @SearchString", YearCode, ProposalStatus, Company, SearchString).AsQueryable().ToList();
它运行正常,并返回所有预期的390,000条记录。
在过去的几个小时中,我已经阅读了所有其他有关Stack Overflow的文章,并尝试了这些解决方案,但似乎都没有效果。