对linq编译查询使用联合
本文关键字:查询 linq 编译 | 更新日期: 2023-09-27 17:52:49
我正在寻找使用linq联合的最佳方法。
目前我必须用相同的签名编译查询:
public Func<DataContext, int, IQueryable<Item>> Query1
public Func<DataContext, int, IQueryable<Item>> Query2
和在我的仓库类我正在做某事链接这个:
using (DataContext context = new DataContext(ConnectionString)) {
return _queries.Query1(context, id).Union(
_queries.Query2(context, id));
}
但是有些东西告诉我联合应该在编译后的查询中。像这样:(这段代码不会编译)
public Func<DataContext, int, IQueryable<Item>> Query1 =
CompiledQuery.Compile((DataContext context, int id) =>
from table1 in context.GetTable<Table1>()
where table1.foreignId = id
select new Item(table1)
union
from table2 in context.GetTable<Table2>()
where table2.foreignId = id
select new Item(table2));
有办法做到这一点吗?
尝试:
public Func<DataContext, int, IQueryable<Item>> Query1 =
CompiledQuery.Compile((DataContext context, int id) =>
(from table1 in context.GetTable<Table1>()
where table1.foreignId = id
select new Item(table1))
.Union(
(from table2 in context.GetTable<Table2>()
where table2.foreignId = id
select new Item(table2))
));