我有一个使用分页的搜索。我的查询带回更多结果,然后我可以每页列出。我担心随着我的网站规模不断扩大,这可能会成为性能问题。我不确定如何有效地解决这个问题,并希望得到一些指导。下面是我调用来执行我的查询的方法。
private List<dynamic> AdminSearchAll(string keyword)
{
string DDL = DDLAddDivision.SelectedValue;
int DDLInt;
int searchID = 0;
if (int.TryParse(DDL, out DDLInt))
{
//int searchID;
if (!int.TryParse(keyword, out searchID))
searchID = -1; // set to an invalid ID
}
ItemContext db = new ItemContext();
var contacts = (from c in db.Contacts
join cat in db.Categories on c.CategoryID equals cat.CategoryID
join div in db.Divisions on c.DivisionID equals div.DivisionID
where
(DDLInt == 1 || c.DivisionID == DDLInt) &&
(c.Deleted == false) &&
//Contains
(
c.ContactName.Contains(keyword) ||
c.ContactEmail.Contains(keyword) ||
c.ContactOPhone.Contains(keyword) ||
c.ContactID.Equals(searchID)
)
select new
{
Name = c.ContactName,
Phone = c.ContactOPhone,
Type = c.Type,
Email = c.ContactEmail,
ID = c.ContactID
});
var items = (from i in db.Item
join cat in db.Categories on i.CategoryID equals cat.CategoryID
join div in db.Divisions on i.DivisionID equals div.DivisionID
where
(DDLInt == 1 || i.DivisionID == DDLInt) &&
(i.Deleted == false) &&
//Contains
(
i.ItemName.Contains(keyword) ||
i.Email.Contains(keyword) ||
i.Description.Contains(keyword) ||
i.ItemID.Equals(searchID)
)
select new
{
Name = i.ItemName,
Phone = i.Phone,
Type = i.Type,
Email = i.Email,
ID = i.ItemID
});
var all = contacts.Union(items);
return all.ToList<dynamic>();
我使用 Asp.net Data Pager 进行分页。我每页显示 10 个结果。我的数据寻呼机使用一种方法 OnPreRender="Pager_PreRender"
protected void Pager_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
string keyword = txtSearch.Text.Trim();
List<dynamic> Cresults = AdminSearchAll(keyword);
}
}
我的问题是使用 LINQ to SQL 将我的查询限制为在我分页时仅返回十个结果而不是返回所有结果并且由于寻呼机而仅显示 10 个的正确方法是什么。PS 我为所有的代码道歉,但我想让你看看我在做什么。