0

嗨,我是 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.

我的项目处于生产阶段,所以我根本没有时间。有谁知道我做错了什么?

4

2 回答 2

1

这应该可以解决您的问题:

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 的答案。

于 2012-12-28T19:47:45.413 回答
0

您正在使用Linq to Entity

它不支持所有功能。支持和不支持的功能列表

您需要编写自定义模型定义函数 GetBlockDetailsForTheDocument才能在 LINQ 查询中使用它。

于 2012-12-28T19:40:56.757 回答