我正在搜索一個sql server數據庫,顯然它比應用程序可以處理更多。我回到了很多結果的路上,我需要儘量減少發回的記錄數量,然後告訴用戶他們選擇了太多的記錄。需要linq查詢返回只有最後100個結果
以下是我有:
private List<Log> SearchLog()
{
try
{
using (var model = new SuburbanPortalEntities())
{
var qry = from logs in model.Logs
where logs.LogDateTime > dateTimePicker_Start.Value &&
logs.LogDateTime < dateTimePicker_End.Value
select logs;
Guid tokenid;
if (Guid.TryParse(textBox_TokenId.Text, out tokenid))
{
qry = qry.Where(x => x.TokenId == tokenid);
}
if (!string.IsNullOrEmpty(textBox_SessionId.Text))
{
qry = qry.Where(x => x.SessionId == textBox_SessionId.Text.ToLower());
}
if (!string.IsNullOrWhiteSpace(textBox_Contains.Text))
{
qry = qry.Where(x => x.Message.Contains(textBox_Contains.Text));
}
if (checkedListBox_DisplayFilter.GetItemChecked(0))
{
qry = qry.Where(x => x.IsWarning);
}
if (checkedListBox_DisplayFilter.GetItemChecked(1))
{
qry = qry.Where(x => x.IsException);
}
var sourceEnumList = new List<string>();
if (checkBox_WebPortal.Checked)
{
sourceEnumList.Add("WebPortal");
}
if (checkBox_SubService.Checked)
{
sourceEnumList.Add("SubService");
}
if (checkBox_TruckRouting.Checked)
{
sourceEnumList.Add("TruckRouting");
}
if (checkBox_SuburbanHub.Checked)
{
sourceEnumList.Add("SuburbanHub");
}
if (sourceEnumList.Any())
{
qry = qry.Where(x => sourceEnumList.Contains(x.SourceEnum));
}
qry = qry.OrderByDescending(x => x.LogDateTime);
return qry.ToList();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return null;
}
}
如何獲得最後100條記錄,知道告訴他們需要優化其搜索用戶?
謝謝!
做到這一點,最好的辦法是通過反轉排序順序拿到前100名。 –