Linq to Entities Does Not Recognize the Method 'Int32 Int32(System.String)' Method, and This Method Cannot Be Translated into a Store Expression

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression

in Linq to Entity, you should use the methods in your query which is supported by your provider to convert them to expression tree to run on your Data Base side.

all providers must support some methods by default called Canonical Functions (Read More Here), and also you can define your user defined function and stored procedure as edm functions to use in linq query (Read More Here) and (Here).

in addition you can use methods which is supported by providers and can be converted to expression tree which are in EntityFunctions and SqlFunctions.

and finally about your question, you can convert UserID and ClassID before your query, like this:

var UID = int.Parse(UserID);
var CID = int.Parse(ClassID);
var record = context.enrollments.SingleOrDefault
(row => row.userId == UID && row.classId == CID);

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method when attempting to parse a column for inequality comparisons

First, I would highly recommend to check your database design, whether there is a really good reason for ID to be a string. I would consider changing the ID DB type to int and you will get rid of this problem with converting.

The error you get means, that EF does not know how to convert the method Int32.Parse() to SQL.
Basically you have two options how to deal with that:

Do the comparison outside the linq to entities:

var myVar= Entity.SetName.AsEnumerable()
.Where(p => int.Parse(p.ID) >= start &&
int.Parse(p.ID) <= end);

But this is not recommended, because you are reading whole result set from DB, before applying the where condition.

Or make custom model defined function as described in this post on SO:

Convert String to Int in EF 4.0
or
Entity Framework: Where do I extend the CSDL/MSL?

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression

Instead of this:

Select(a => Convert.ToInt32(a.RoleId))

Do this:

Select(a => a.RoleId.Value)

The reason is in the error description; when you are doing these queries through IQueryable, the methods being used within the selector have to be something that can be translated into a SQL query or function. In this case Convert.ToInt32() is not such a method. For int fields with null allowed, using the .NET .Value property does work, though.

Note that this would not work if your RoldId is null, however. You'll get an InvalidOperationException. You might want to return a set value instead if the backing field is null:

Select(a => a.RoleId.HasValue ? a.RoleId.Value : int.MinValue)

This will return the value if there is one, and int.MinValue if not.

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

public IEnumerable<CourseNames> GetCourseName()
{
var courses = from o in entities.UniversityCourses
select new { o.CourseID, o.CourseName };

return courses.ToList() // now we have in-memory query
.Select(c => new CourseNames()
{
CourseID = Convert.ToInt32(c.CourseID), // OK
CourseName = c.CourseName
});
}

c#, LINQ to Entities does not recognize the method 'Int32

Try querying the data you need, then converting the collection to IEnumerable<T>, then using the CalculateAge() method.

var employee = db.Employee.Where(x => x.EmployeeId == id ).
Select(e => new { e.EmployeeId, e.FirstName, e.LastName, e.DOB, e.Department.DepartmentName }).
AsEnumerable().
Select(b => new
{
Id = b.EmployeeId,
Name = b.FirstName + " " + b.LastName,
Age = CalculateAge(b.DOB.ToString()),
Department = b.DepartmentName
} ).Tolist();
if employee != null)
{
dgvEmployee.DataSource = employee;
}

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

exclude Convert.ToInt32(list.ElementAt(i).userid)) from your linq.

for (int i = 0; i < list.Count; i++)
{
int friendId = Convert.ToInt32(list.ElementAt(i).userid);
var doesrequestExist = cxt.Friends.FirstOrDefault(u => (u.User_Id == incID) &&
(u.Friend_UserId == friendId &&
(u.Request_Status == 0 || u.Request_Status == 1));

if (doesrequestExist != null)
{
}
}

linq to entities does not recognize the method int32 toint32

It looks like CodePostal.Text is something within your existing context - so all you've got to do is extract that from the query:

int code = Convert.ToInt32(CodePostal.Text); // Or use int.Parse...

// Not using a query expression here as it just adds extra cruft
IEnumerable<Commune> myCommunes = db.Communes.Where(d => d.CodePostal == code);

It's not clear where CommunesList comes from - but if it's empty before this, you could just use:

CommunesList = db.Communes.Where(d => d.CodePostal == code).ToList();


Related Topics



Leave a reply



Submit