Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities does not recognize the method and this method cannot be translated into a store expression

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

LINQ to Entities does not recognize the method 'SchoolBreifcase.Compliance get_Item(Int32)' method, and this method cannot be translated into a store expression.

Here is my full code

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;
        }

This line give that error:

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 does not recognize the method

etc..But does not help to me .So I asked this question . Please don't any one close this question for reason of already asked

like image 767
Ramesh Rajendran Avatar asked Apr 04 '13 13:04

Ramesh Rajendran


People also ask

Does LINQ to entities not recognize the method system string tostring?

LINQ to Entities does not recognize the method System.String ToString () method, and this method cannot be translated into a store expression. In this article I will explain with an example, how to solve the following error (exception) when using LINQ to SQL or Lamba expressions on DbSet records returned by Entity Framework in C# .Net.

Why does LINQ to Entity Framework not work?

LINQ to Entities does not recognize the method System.String ToString () method, and this method cannot be translated into a store expression. In following code snippet, a LINQ to SQL query is executed on the records returned from the Entity Framework.

Why LINQ to SQL query is not working?

This exception occurs because LINQ to SQL query internally builds an SQL query and executes it on the database directly and hence when one tries to use the ToString method which is a .Net data type conversion method and cannot be used for an SQL Query. 1. StringConvert method

Why do I get an exception when I call tostring in LINQ?

var result=dc.Tests.Where (i=>i.status==status.ToString ()).FirstOrDefault (); This exception mainly occurs when we query SQL database using entity framework. The problem is that you are calling ToString in a LINQ to Entities query (IQueryable to IEnumrable).


2 Answers

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;
    }
}
like image 102
Satpal Avatar answered Oct 12 '22 20:10

Satpal


It's about the compliance[i].ComplianceId. Create a variable first:

var id = compliance[i].ComplianceId;

financialCompliance = datamodel.FinancialCompliances
                      .Where(f => f.ComplianceId == id).SingleOrDefault();
like image 36
Gert Arnold Avatar answered Oct 12 '22 19:10

Gert Arnold