Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何简化LINQ到实体查询并使其动态化_Asp.net_Linq_Entity Framework 4 - Fatal编程技术网

Asp.net 如何简化LINQ到实体查询并使其动态化

Asp.net 如何简化LINQ到实体查询并使其动态化,asp.net,linq,entity-framework-4,Asp.net,Linq,Entity Framework 4,我有一个通过Entity Framework获取数据的查询,该查询现在已经运行了。问题是,我想动态地创建查询,即在块中构建查询,但我不知道如何准确地创建查询。我相信,如果我能以某种方式保存一开始检索到的某个值,我就可以这样做,但现在我检索了两次(可能也有点慢?除非编译器在查询之前修复它)。有道理吗?以下是LINQ: from d in db.trains where d.cancelled && d.station == toStation && d.date &

我有一个通过Entity Framework获取数据的查询,该查询现在已经运行了。问题是,我想动态地创建查询,即在块中构建查询,但我不知道如何准确地创建查询。我相信,如果我能以某种方式保存一开始检索到的某个值,我就可以这样做,但现在我检索了两次(可能也有点慢?除非编译器在查询之前修复它)。有道理吗?以下是LINQ:

from d in db.trains
where d.cancelled
&& d.station == toStation
&& d.date >= fromDate.Date
&& d.date <= toDate.Date
&& (from k in db.trains
    where 
    k.date == d.date
    && k.trainId == d.trainId
    && k.stationId == fromStation
    && k.position <= d.position
    select k.trainId).Contains(d.trainId)
select new
{
  trainId = d.trainId,
  date = d.date,
  arrival = d.arrival,
  departure = (from k in db.trains
               where
               k.date == d.date
               && k.trainId == d.trainId
               && k.stationId == fromStation
               && k.position <= d.position
               select k.departure).FirstOrDefault()
}
);

因此,基本上,我需要从数据库中检索两个对象,其中第一个具有stationId x,另一个具有stationId y,并且两者都具有相同的日期和trainId,并且它们必须根据位置以正确的顺序排列(列车双向行驶,但具有不同的trainId)

此外,我希望能够动态构建此查询,如下所示:

trainId stationId date       arrival departure position

1       99        2010-10-11 10:00   10:10     1
1       98        2010-10-11 11:20   11:30     2
1       47        2010-10-11 12:30   12:40     3
2       99        2010-10-10 15:00   15:10     5
var trains = from d in db.trains
             select d;

if (id > 0)
  trains = trains.Where(p => p.trainId == id);

if (date != DateTime.MinValue)
  trains = trains.Where(p => p.date == date);

var items = (from train in trains).ToList();
from d in db.trains
where d.cancelled
&& d.station == toStation
&& d.date >= fromDate.Date
&& d.date <= toDate.Date
let departures = (from k in db.trains
    where 
    k.date == d.date
    && k.trainId == d.trainId
    && k.stationId == fromStation
    && k.position <= d.position
    select k)
where departures.Select(d => d.trainId).Contains(d.trainId)
select new
{
  trainId = d.trainId,
  date = d.date,
  arrival = d.arrival,
  departure = departures.Select(d => d.departure).FirstOrDefault()
}
);
即基于各种变量是否有值。

您可以使用该语句存储局部变量,并在以后重用它们
大概是这样的:

trainId stationId date       arrival departure position

1       99        2010-10-11 10:00   10:10     1
1       98        2010-10-11 11:20   11:30     2
1       47        2010-10-11 12:30   12:40     3
2       99        2010-10-10 15:00   15:10     5
var trains = from d in db.trains
             select d;

if (id > 0)
  trains = trains.Where(p => p.trainId == id);

if (date != DateTime.MinValue)
  trains = trains.Where(p => p.date == date);

var items = (from train in trains).ToList();
from d in db.trains
where d.cancelled
&& d.station == toStation
&& d.date >= fromDate.Date
&& d.date <= toDate.Date
let departures = (from k in db.trains
    where 
    k.date == d.date
    && k.trainId == d.trainId
    && k.stationId == fromStation
    && k.position <= d.position
    select k)
where departures.Select(d => d.trainId).Contains(d.trainId)
select new
{
  trainId = d.trainId,
  date = d.date,
  arrival = d.arrival,
  departure = departures.Select(d => d.departure).FirstOrDefault()
}
);
来自d,单位为db.trains
d.取消的地方
&&d.站==toStation
&&d.date>=fromDate.date
&&d.日期d.出发)。第一个默认值()
}
);
您可以使用该语句存储局部变量,并在以后重用它们
大概是这样的:

trainId stationId date       arrival departure position

1       99        2010-10-11 10:00   10:10     1
1       98        2010-10-11 11:20   11:30     2
1       47        2010-10-11 12:30   12:40     3
2       99        2010-10-10 15:00   15:10     5
var trains = from d in db.trains
             select d;

if (id > 0)
  trains = trains.Where(p => p.trainId == id);

if (date != DateTime.MinValue)
  trains = trains.Where(p => p.date == date);

var items = (from train in trains).ToList();
from d in db.trains
where d.cancelled
&& d.station == toStation
&& d.date >= fromDate.Date
&& d.date <= toDate.Date
let departures = (from k in db.trains
    where 
    k.date == d.date
    && k.trainId == d.trainId
    && k.stationId == fromStation
    && k.position <= d.position
    select k)
where departures.Select(d => d.trainId).Contains(d.trainId)
select new
{
  trainId = d.trainId,
  date = d.date,
  arrival = d.arrival,
  departure = departures.Select(d => d.departure).FirstOrDefault()
}
);
来自d,单位为db.trains
d.取消的地方
&&d.站==toStation
&&d.date>=fromDate.date
&&d.日期d.出发)。第一个默认值()
}
);

Aha,我知道会有这样的事情:)完美。只有一个问题,我如何从“偏离”列表中检索不同的值,即首先检查它是否包含trainId为x的trainobject,然后检索偏离变量?我想我需要先将其存储为列车列表,而不是ID列表,但如何在该列表上运行contains?@Johan,对不起,快速阅读以查看不同子查询中的不同字段,我更新了答案,将整个列车存储在发车列表中。谢谢Albin,这非常有效!我现在已经运行了一些测试,我看到了一个奇怪的模式。我这样做的第一个查询通常比旧方法慢(尤其是对于更大的结果),但在第一个查询之后,新查询通常更快。你知道为什么会这样吗,是sql server缓存查询更好还是什么的?它既是运行时查询生成(将LINQ转换为sql语句)又是服务器缓存查询结构。谢谢Adam,我猜是这样的。我想这是有道理的!阿尔宾,你对我问题的第二部分,如何使查询动态化,有什么建议吗?啊哈,我知道会有这样的建议:)完美。只有一个问题,我如何从“偏离”列表中检索不同的值,即首先检查它是否包含trainId为x的trainobject,然后检索偏离变量?我想我需要先将其存储为列车列表,而不是ID列表,但如何在该列表上运行contains?@Johan,对不起,快速阅读以查看不同子查询中的不同字段,我更新了答案,将整个列车存储在发车列表中。谢谢Albin,这非常有效!我现在已经运行了一些测试,我看到了一个奇怪的模式。我这样做的第一个查询通常比旧方法慢(尤其是对于更大的结果),但在第一个查询之后,新查询通常更快。你知道为什么会这样吗,是sql server缓存查询更好还是什么的?它既是运行时查询生成(将LINQ转换为sql语句)又是服务器缓存查询结构。谢谢Adam,我猜是这样的。我想这是有道理的!阿尔宾,你对我问题的第二部分,如何使查询动态化,有什么建议吗?