find and solve || findandsolve.com
Please wait.....

The type System.Linq.Iqueryable cannot be translated into a LINQ to Entities store expression

The type System.Linq.Iqueryable cannot be translated into a LINQ to Entities store expression

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The specified method 'Entities.TaskProgress Max[TaskProgress](System.Linq.IQueryable`1[SMS.SMS.Entities.TaskProgress])' on the type 'System.Linq.Queryable' cannot be translated into a LINQ to Entities store expression because no overload matches the passed arguments.

Here is my coode

  public static string TotalProgressByTaskId(int taskId)
        {
            var user = CommonService.UserManager.CustomeUserManager.GetUserDetails();
            SMSEntities _ent = new SMSEntities();
            var result = _ent.TaskProgress.Where(x => x.SchoolCode == user.SchoolCode
            && x.TaskId == taskId
            && x.DeletedDate == null).Max().ProgressValue;
            if (string.IsNullOrWhiteSpace(result))
            {
                return "0";
            }
            return result;
        }


Solutions


Solution: 1

Try this

 var user = CommonService.UserManager.CustomeUserManager.GetUserDetails();
            SMSEntities _ent = new SMSEntities();
            var result = _ent.TaskProgress.Where(x => x.SchoolCode == user.SchoolCode
            && x.TaskId == taskId
            && x.DeletedDate == null).Max(x=>x.ProgressValue);
            if (string.IsNullOrWhiteSpace(result))
            {
                return "0";
            }
            return result;

Solution: 2

You aren't asking for the highest ProgressValue, you're trying to get the highest TaskProgress, and getting its ProgressValue. EF does not understand what "the highest Reservation" means.


var result= _ent.TaskProgress.Max(r => r.ProgressValue);

or

var result= _ent.TaskProgress.Select(r => r.ProgressValue).Max();

Solution: 3

if in your table or list where data is null you  should  replace null with 0

var result=_ent.TaskProgress.Max(u =>(int?)u.ProgressValue) ?? 0;

Solution: 4

Some Example Max (LINQ)

Max for Numeric Types

var numbers = new List<int> { 1, 5, 3,7, 2 };

int maxNumber = numbers.Max(); // maxNumber: 7

Gets maximal number from list of decimal numbers.

var numbers = new List<decimal> { 1.0m, 7.1m, 4.32, 2.0m };
decimal maxNumber = numbers.Max(); // maxNumber: 7.1

Calling Max on empty collection throws exception.

var numbers = new List<int>();  // this is empty list

int maxNumber = numbers.Max(); // throws InvalidOperationException here

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response