嗨,我是Linq和实体框架的新手。我正在做这样的事情我有3个视图模型:
1.
public class FlowViewModel
{
..........................
public List<FlowLevelViewModel> Levels { get; set; }
}
public class FlowLevelViewModel
{
.........................
public List<BlockDetailsViewmodel> Blocks { get; set; }
}
public class BlockDetailsViewmodel
{
.......................
}
我从我的控制器呼叫数据层。
var model = new FlowViewModel();
model = dataOb.GetFlowForTheDocument(company, docType);
model = dataOb.GetFlowStageForTheDocument(model);
return model;
在我的数据层中
public FlowViewModel GetFlowStageForTheDocument(FlowViewModel model)
{
var flowlevelviewModel = (from p in dbContext.FlowStages
where p.FlowID == model.FlowId
select new FlowLevelViewModel()
{
.................
Blocks = GetBlockDetailsForTheDocument(p.StageID, .StageType)
}).ToList();
model.Levels = flowlevelviewModel;
return model;
}
public List<BlockDetailsViewmodel> GetBlockDetailsForTheDocument(int StageID, string stageType)
{
var blockDetails = new List<BlockDetailsViewmodel>();
......................................
return blockDetails;
}
当我运行程序时,我得到了这个错误:
**NotSupportedException Was unhandled by user Code**
LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[SEADViewModel.BlockDetailsViewmodel] GetBlockDetailsForTheDocument(Int32, System.String)' method, and this method cannot be translated into a store expression.
我的项目正处于生产阶段,所以我根本没有时间。有人知道我做错了什么吗?
这应该可以解决您的问题:
var data = (from p in dbContext.FlowStages
where p.FlowID == model.FlowId
select p).ToList();
var flowlevelviewModel = (from p in data
select new FlowLevelViewModel()
{
.................
Blocks = GetBlockDetailsForTheDocument(p.StageID, .StageType)
}).ToList();
请注意,这将在第一个ToList()
处评估查询。如果需要同时运行整个查询,则需要构建一个简单的LINQ表达式,不能在查询中使用方法GetBlockDetailsForTheDocument
。请参阅@Tilak的答案,以获取支持的内置方法的链接。
您正在使用Linq to Entities。
它并不支持所有功能。支持和不支持的功能列表
您需要编写自定义模型定义的函数GetBlockDetailsForTheDocument
才能在LINQ查询中使用它。