我正在尝试找到与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));
有没有办法实现这个目标?
答案 0 :(得分:3)
尝试:
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))
));