LINQ查询错误- foreach循环中的where子句
本文关键字:where 子句 循环 foreach 查询 错误 LINQ | 更新日期: 2023-09-27 17:49:31
我写了一个LINQ查询,但是我在foreach
循环中执行where
子句时遇到了一些问题。
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
Table<NOTIF_SCHED> NOTIF_SCHED_alias = db.GetTable<NOTIF_SCHED>();
IQueryable<NOTIF_SCHED> notif_sched_data = from sched in NOTIF_SCHED_alias select sched;
foreach (var notif_sched_data_value in notif_sched_data)
{
string a = notif_sched_data_value.NOTIF_RPT_ID.ToString();
Table<mainframe_replication> mainframe_replications_alias = db.GetTable<mainframe_replication>();
IQueryable<mainframe_replication> mainframe_replications_data =
from mfrepl in mainframe_replications_alias
where (mfrepl.RPT_ID.Equals(a))
select mfrepl;
foreach (var mainframe_replication_data_value in mainframe_replications_data)
{
Console.WriteLine("hi");
}
}
}
我不能在行中使用Where
子句:
IQueryable<mainframe_replication> mainframe_replications_data =
from mfrepl in mainframe_replications_alias
where (mfrepl.RPT_ID.Equals(a))
select mfrepl;**
请帮忙检查一下语法是否有问题
这应该做同样的事情,并且运行得更快:
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
var ids = db.NOTIF_SCHEDs.Select(x=>x.NOTIF_RPT_ID).ToArray();
var repl = db.mainframe_replication
.Where(mfrepl=>ids.Contains(mfrepl.RPT_ID));
foreach (var mainframe_replication_data_value in repl)
{
Console.WriteLine("hi");
}
}
}
如果NOTIF_RPT_ID不是字符串,而RPT_ID是字符串,你可以这样做:
using (DataClasses1DataContext db = new DataClasses1DataContext(("ConnectionString")))
{
var ids = db.NOTIF_SCHEDs.Select(x=>x.NOTIF_RPT_ID.ToString()).ToArray();
var repl = db.mainframe_replication
.Where(mfrepl=>ids.Contains(mfrepl.RPT_ID));
foreach (var mainframe_replication_data_value in repl)
{
Console.WriteLine("hi");
}
}
}