Linq在EntityFramework中的铸造问题

时间:2012-12-28 19:38:11

标签: asp.net-mvc asp.net-mvc-3 linq entity-framework

您好, 我是Linq和实体框架的新手。我正在做这样的事情 我有3个viewmodel:

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.

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

2 个答案:

答案 0 :(得分: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的答案,获取支持的构建方法的链接。

答案 1 :(得分:0)

您正在使用Linq to Entities

它不支持所有功能。 List of supported and non supported functions

您需要编写custom model defined function GetBlockDetailsForTheDocument以在LINQ查询中使用它。