0

在 MVVM 中,我从存储库获取一个实体集到 viewmodel。在尝试访问实体的元素时,它会引发异常:

LINQ to Entities 无法识别方法 'InsurableRisk.Entities.QueriesParameter ElementAt[QueriesParameter](System.Linq.IQueryable`1[InsurableRisk.Entities.QueriesParameter], Int32)' 方法,并且此方法无法转换为存储表达式。

这是我的代码:

存储库:

public IQueryable<QueriesParameter> GetParams(int QKey)
    {
        IQueryable<QueriesParameter> param = (from Q in Context.QueriesParameters
                                              where (Q.QueryKey == QKey)
                                              select Q);
        return param;
    }

服务:

public IQueryable<QueriesParameter> GetParams(int QKey)
    {
        return _repository.GetParams(QKey);
    }

视图模型:

    paramLabel = new string[] { "ParamLabel1", "ParamLabel2", "ParamLabel3", "ParamLabel4", "ParamLabel5", "ParamLabel6" };
    param = new string[] { "Param1", "Param2", "Param3", "Param4", "Param5", "Param6" };
    paramVisibility = new string[] { "ParamVisiblity1", "ParamVisiblity2", "ParamVisiblity3", "ParamVisiblity4", "ParamVisiblity5", "ParamVisiblity6" };
    paramLabelVisibility = new string[] { "ParamLabelVisiblity1", "ParamLabelVisiblity2", "ParamLabelVisiblity3", "ParamLabelVisiblity4", "ParamLabelVisiblity5", "ParamLabelVisiblity6" };

    private Dictionary<int, string> m_queryNames;
    private Dictionary<int, string> m_ReadOnlyQueryNames;
    private int m_SelectedQueryNames;

    public int SelectedQueryNames
    {
        get
        {
            return m_SelectedQueryNames;
        }
        set
        {
            if (m_SelectedQueryNames != value)
            {
                m_SelectedQueryNames = value;
                OnPropertyChanged("SelectedQueryNames");
                var QKey = m_SelectedQueryNames;
                var sqlQuery = _service.GetQuery(QKey);
                var paramCount = _service.GetParamCount(QKey);
                //code to make the run button visible and the parameters to be visible  
                m_Visibility = true;
                IQueryable<QueriesParameter> param = _service.GetParams(QKey);
                for (int i = 1; i <= paramCount; i++)
                {
                    OnPropertyChanged(paramLabelVisibility[i]);
                    OnPropertyChanged(paramVisibility[i]);
                    QueriesParameter qParam = param.ElementAt(i);  <!-- I get the exception here -->
                    m_LabelName = qParam.ParameterName;
                    OnPropertyChanged(paramLabel[i]);
                    //OnPropertyChanged(param[i]);                        
                }                    
            }
        }

为什么我会收到此错误的任何帮助?

4

1 回答 1

1

您收到此错误ElementAt是因为 LINQ to Entities 不支持(实际上,您将如何将其转换为 SQL?)。这是支持和不支持的 LINQ 方法的列表。

您可以改为枚举参数:

IQueryable<QueriesParameter> param = _service.GetParams(QKey);
int i = 1; // btw why you are iterating from index 1? It should be zero!
foreach(var p in param)
{
   OnPropertyChanged(paramLabelVisibility[i]);
   OnPropertyChanged(paramVisibility[i]);
   QueriesParameter qParam = p; // here
   m_LabelName = qParam.ParameterName;
   OnPropertyChanged(paramLabel[i]);
   i++;
}    

AsEnumerable()另一种选择 - 通过调用或将查询移动到客户端ToList()。然后将使用 Linq to Objects,您可以在其中使用ElementAt(index)方法或通过 indexer [index]

 List<QueriesParameter> param = _service.GetParams(QKey).ToList(); 
 //... your code
 QueriesParameter qParam = param[i];    
于 2013-06-17T18:50:20.193 回答