【发布时间】:2016-08-12 10:17:08
【问题描述】:
我正在使用实体框架和 Linq。我需要对我的对象的 2 个属性进行查询。
我在数据库中有这个对象(大约 200.000 条记录):
public class DeviceState
{
public int ID { get; set; }
public DateTime TimeStamp { get; set; }
public string StatusCode { get; set; }
public int Device_ID { get; set; }
}
我需要这样查询:
List<DeviceState> listState = systemDB.DeviceStates.Where(s => s.Device_ID == DeviceID).Where(l => l.TimeStamp > startDate).Where(l => l.TimeStamp < endDate).Where(s => s.StatusCode == "xx").ToList();
foreach (DeviceState status in listState)
{
// here I need to save in an object the status code and the time stamp:
object.StatusCode= status.StatusCode;
object.TimeStamp = status.TimeStamp;
}
此查询需要很长时间(大约 15 分钟)。我认为这是由于创建了列表。所以我尝试了这个:
foreach (DeviceState status in systemDB.DeviceStates.Where(s => s.Device_ID == DeviceID).Where(l => l.TimeStamp > startDate).Where(l => l.TimeStamp < endDate).Where(s => s.StatusCode == "xx"))
{
// here I need to save in an object the status code and the time stamp:
object.StatusCode= status.StatusCode;
object.TimeStamp = status.TimeStamp;
}
创建列表的速度要快得多。但是由于 foreach 循环,我仍然存在性能问题。每个元素需要 5ms。
我需要找到一个需要几秒钟来执行的解决方案。
【问题讨论】:
标签: c# entity-framework linq