同僚のコードに出くわし、おそらく非効率的だと思いました
bool any = (from c in listDeviceMaxDate
where c.DeviceKey == m_deviceList[i].deviceKey
select c).Any();
if (!any)
{
latestDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
}
else
{
// from the list we have get the lastest max date from the flow table
DeviceDateTimeItem temp = (from c in listDeviceMaxDate
where c.DeviceKey == m_deviceList[i].deviceKey
select c).First();
latestDate = Convert.ToDateTime(temp.dateTimeMax);
}
First()
私の最初の本能は、linqクエリを保存し、必要に応じて参照することですが、制約のないクエリが行うすべての行を実際にlinqが取得するのを演算子が妨げる可能性があることに気付きました。
コードの再構築について最初に考えた方法:
var deviceList = from c in listDeviceMaxDate
where c.DeviceKey == m_deviceList[i].deviceKey
select c;
if (!deviceList.Any())
{
latestDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
}
else
{
// from the list we have get the lastest max date from the flow table
DeviceDateTimeItem temp = deviceList.First();
latestDate = Convert.ToDateTime(temp.dateTimeMax);
}
私の質問はFirst()
、2 番目の linq クエリを呼び出すと、すべての結果が返されなくなるのでしょうか。実際には、元の方法で行う方が速いのでしょうか?