我有一段代碼表現糟糕,需要重新引導它以在啓動.ToList之前引入適當的where子句,但是,這就是我卡住的地方。性能不佳的查詢需要重寫
目前的代碼看起來這艾克(約,我已經採取了一些搜索標準出來,使其更容易顯示)
var Widgets = from b in _caspEntities.Widgets.Include("WidgetRegionLogs")
.Include("WidgetStatusLogs").Include("WidgetVoltageTests")
select b;
IEnumerable<Widget> results = Widgets.ToList();
if (comboBoxRegion.SelectedValue.ToString() != "0")
{
results = from b in results
where b.CurrentRegionLog != null && b.CurrentRegionLog.RegionId == int.Parse(comboBoxRegion.SelectedValue.ToString())
select b;
}
if (comboBoxStatus.SelectedValue != null)
{
results = from b in results
where b.CurrentStatusLog != null && b.CurrentStatusLog.StatusId == comboBoxStatus.SelectedValue.ToString()
select b;
}
if (txtCode.Text.Trim().Length > 0)
{
results = from b in results
where b.CodeNumber == txtCode.Text.Trim()
select b;
}
dataGridViewWidget.DataSource = results.ToList();
我可以寫SQL容易不夠,基本上是模型簡單,我有一個Widget它有一個RegionLog和一個StatusLog,兩者都存儲歷史記錄。通過按WidgetID進行分組並選擇最新的更新日期(然後轉到區域和狀態表以獲取實際值),從中檢索當前區域和狀態。
所以,我需要將其轉換成LINQ,但說實話,我沒有線索,但肯和願意學習。在我的腦海中,我想我需要在where子句中添加更好的一些,然後在應用where子句之後再執行Widget.toList。我正在努力與CurrentRegionLog
和CurrentStatusLog
的概念,因爲它們沒有填充,直到我運行IEnumerable
。
如果任何人都可以給一些指點,我會很感激,感謝
編輯 - 添加
public BatteryRegionLog CurrentRegionLog
{
get { return _currentRegionLog; }
}
private BatteryRegionLog _currentRegionLog
{
get
{
if (this.BatteryRegionLogs.Count > 0)
{
BatteryRegionLog log = this.BatteryRegionLogs.OrderByDescending(item => item.LastModifiedDate).First();
return log;
}
else
{
return null;
}
}
}
感謝格特,我已經試過這是通過得到以下錯誤..指定的類型成員'CurrentRegionLog'不支持LINQ to Entities。只支持初始化,實體成員和實體導航屬性。我想我需要設置這個地方,但我抓住了一些吸管:-( – MikeH 2013-02-20 10:29:50
「CurrentRegionLog」背後的代碼是什麼?它應該是一個導航屬性。 – 2013-02-20 10:31:55
私人BatteryRegionLog _currentRegionLog { 得到 { 如果(this.BatteryRegionLogs.Count> 0){ BatteryRegionLog日誌= this.BatteryRegionLogs.OrderByDescending(項目=> item.LastModifiedDate)。首先(); 返回日誌; } else { return null; }} } 在 – MikeH 2013-02-20 10:34:19