我写了一个LINQ查询但是在where
循环中执行foreach
子句时遇到了一些问题。
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;**
有人可以帮助并检查语法是否错误。
答案 0 :(得分:0)
这应该做同样的事情并且运行得更快:
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");
}
}
}