Linq Error: InvalidOperationException: Could not translate expression
By : Dustin V
Date : March 29 2020, 07:55 AM
will be helpful for those in need Basically what's happening here is that LINQ to SQL is taking your entire query and trying to convert it into something that SQL Server can understand. The problem, though, is that SQL Server has no concept of DateTime.ToShortDateString, so the conversion to SQL fails. You'll have to change your query so that it just selects SellEndDate (which will get it as a Nullable ) and then when you use the results of that query you can do the conversion to string. For example: code :
var list = (from p in Products
select p.SellEndDate).ToList();
// calling ToList() above means we have the entire resultset in memory and
// no longer have to pass the query back to SQL Server
var stuff = from p in list select new
{
selldate = p.SellEndDate == null ?
string.Empty :
p.SellEndDate.Value.ToShortDateString()
};
|
Expression.Call on Expression.PropertyOrField on Interface IList<T> throws InvalidOperationException
By : Mkid
Date : March 29 2020, 07:55 AM
around this issue It behave like that because of how "inheritance" in interfaces works in .net. Imagine that you have interfaces like that: code :
public interface ITest
{
string Property{get;set;}
}
public interface ISubTest : ITest
{
}
typeof(ITest).GetProperty("Property"); // returns property
typeof(ISubTest).GetProperty("Property"); // returns null
|
Linq To SQL Any Throws InvalidOperationException: Sequence contains no elements
By : cy6057
Date : March 29 2020, 07:55 AM
like below fixes the issue The fix for this was to not use a class variable to save the context and instead instantiate the context locally in each method wrapped in a using.
|
Aggregate Function in LINQ expression throws error. (cannot be translated into a store expression.)
By : T.Kelly
Date : March 29 2020, 07:55 AM
this one helps. You're having an IQueryable that translates to SQL. Your Aggregate is a method that is unknown to SQL, so there is no way to translate it and you get your exception. A possible way is to call AsEnumerable() before. This will cause the query to execute and get the data from your SQL server, and the remaining actions are executed in memory (and not on your SQL Server). code :
myQuery.AsEnumerable().Aggregate(...)
|
System.InvalidOperationException(An exception was thrown while attempting to evaluate a LINQ query parameter expression)
By : Barabix
Date : March 29 2020, 07:55 AM
I wish this help you I tried to reproduce your problem with the following code and it worked. Make sure that your object AndroidUser is being sent to your method. code :
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Text;
using WebApplicationLab.Models;
using WebApplicationLab.Utils;
namespace WebApplicationLab.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegisterController : ControllerBase
{
private readonly AppDbContext _context;
//Inject your DbContext object instead of create a new instance.
public RegisterController(AppDbContext context)
{
_context = context;
}
public string Post([FromBody]User value)
{
//Make sure that your object `User` is being sent to your method.
if(value == null)
{
return JsonConvert.SerializeObject("Please inform a valid user!");
}
if (!IsUserExists(value.UserName))
{
var random = new Random();
var hash = Common.GetRandomHash(random.Next(1, 100));
var user = new User()
{
UserName = value.UserName,
Hash = Convert.ToBase64String(hash),
Password = Convert.ToBase64String(Common.HashPassword(Encoding.ASCII.GetBytes(value.Password), hash))
};
_context.Users.Add(user);
_context.SaveChanges();
return JsonConvert.SerializeObject("Register Successfully");
}
return JsonConvert.SerializeObject("User already exists.");
}
private bool IsUserExists(string userName)
{
return _context.Users.Any(x => x.UserName.Equals(userName));
}
}
}
[Table("Users")]
public class User
{
[Key]
public string UserName { get; set; }
public string Hash { get; set; }
public string Password { get; set; }
}
|