LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式 [英] LINQ to Entities does not recognize the method and this method cannot be translated into a store expression

查看:61
本文介绍了LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架在数据库中调用某个日期.但是我的下面代码给出了这个错误

I call some date in my database using entity frame work. but My below code giving this error

LINQ to Entities无法识别方法'SchoolBreifcase.Compliance get_Item(Int32)',并且该方法无法转换为商店表达式.

这是我的完整代码

FinancialCompliance financialCompliance = new FinancialCompliance();
            List<Compliance> compliance = null;
            if (HttpContext.Current.User.IsInRole("SchoolAdmin"))
            {

                compliance = datamodel.Compliances.Where(u => u.UserId == userId).OrderBy(c => c.AddedDate).ToList();

            }
            if (HttpContext.Current.User.IsInRole("User"))
            {
                compliance = datamodel.Compliances.Where(u => u.VerifierId == userId || u.OwnerId == userId).OrderBy(c => c.AddedDate).ToList();
            }
            if (compliance != null)
            {
                for (int i = 1; i < compliance.Count; i++)
                {
                    financialCompliance = datamodel.FinancialCompliances.Where(f => f.ComplianceId == compliance[i].ComplianceId).SingleOrDefault();
                    if (compliance.Count == i)
                    {
                        return financialCompliance;
                    }
                }
            }
            return financialCompliance;
        }

此行给出该错误:

financialCompliance = datamodel.FinancialCompliances.Where(f => f.ComplianceId == compliance[i].ComplianceId).SingleOrDefault();

无助于叠加流量答案 我在这个堆栈溢出站点中找到了一些答案

Does not help stack over flow answer I have find some answers in this stack overflow site for

LINQ to Entities无法识别该方法

等.但是对我没有帮助.所以我问了这个问题.

推荐答案

您需要创建一个变量来引用compliance[i].ComplianceId,然后再使用它.

You need to create a variable to refer to compliance[i].ComplianceId then use it later.

for (int i = 1; i < compliance.Count; i++)
{
    var complianceId = compliance[i].ComplianceId;
    financialCompliance = datamodel.FinancialCompliances.Where(f => f.ComplianceId == complianceId ).SingleOrDefault();
    if (compliance.Count == i)
    {
        return financialCompliance;
    }
}

这篇关于LINQ to Entities无法识别该方法,并且该方法无法转换为商店表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆