我一直在尝试将LINQKit整合到共享数据访问层中,但是,我遇到了障碍.使用a构造嵌套查询时ExpandableQuery
,表达式解析器无法正确解包ExpandableQuery
并构造有效的SQL查询.抛出的错误如下:
System.NotSupportedException:无法创建"Store"类型的常量值.在此上下文中仅支持基元类型或枚举类型.
通过以下示例程序,我们可以轻松地重建此问题,并且它AsExpandable()
与`table上的调用明显隔离.
class Program
{
static void Main(string[] args)
{
Database.SetInitializer<MyContext>(null);
var cs = MY_CONNECTION_STRING;
var context = new MyContext(cs);
var table = (IQueryable<Store>)context.Set<Store>();
var q = table
.AsExpandable()
.Select(t => new {Id = t.StoreId, less = table.Where(tt => tt.StoreId > t.StoreId) })
.Take(1)
.ToArray();
}
}
public class MyContext : DbContext
{
public MyContext(string connection) : base(connection) {}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Store>();
base.OnModelCreating(modelBuilder);
}
}
[Table("stores")]
public …
Run Code Online (Sandbox Code Playgroud)