How can I solve this problem?
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.Threading.Tasks.Task`1[Jahan.Blog.Model.Identity.User] FindByIdAsync(Int32)' method, and this method cannot be translated into a store expression.
public virtual User User { get; set; }
private IQueryable<ArticleGridViewModel> Query()
{
ArticleRepository repository = new ArticleRepository();
IQueryable<ArticleGridViewModel> query = repository.FindAll().Select(article => new ArticleGridViewModel
{
Tags = article.ArticleTags.Where(c => c.ArticleId == article.Id).Select(b => b.Tag).Distinct().ToList(),
NumberOfComments = article.Comments.Count(c => c.ArticleId == article.Id),
AttachmentFiles =article.AttachmentFiles.Where(a => a.ArticleId == article.Id).Distinct().ToList(),
CreatedDate = article.CreatedDate,
IsActive = article.IsActive,
IsActiveNewComment = article.IsActiveNewComment,
LikeCounter = article.LikeCounter,
ModifiedDate = article.ModifiedDate,
RateCounter = article.RateCounter,
Title = article.Title,
UserId = article.UserId,
Comments = Comments.Where(c => c.ArticleId == article.Id).ToList(),
User = AppUserStore.Instance.FindByIdAsync(article.Id).Result, // The error happened because of this line of code.
});
return query;
}
public virtual IQueryable<ArticleGridViewModel> QueryByCriteria(Expression<Func<ArticleGridViewModel, bool>> predicate = null, params Expression<Func<ArticleGridViewModel, object>>[] includeProperties)
{
IQueryable<ArticleGridViewModel> items = Query();
if (includeProperties != null)
{
foreach (var includeProperty in includeProperties)
{
items = items.Include(includeProperty);
}
}
if (predicate != null)
return items.Where(predicate);
return items;
}
public virtual IEnumerable<ArticleGridViewModel> FindAll(Expression<Func<ArticleGridViewModel, bool>> predicate = null, params Expression<Func<ArticleGridViewModel, object>>[] includeProperties)
{
List<ArticleGridViewModel> result = QueryByCriteria(predicate, includeProperties).ToList();
return result;
}
Everything inside of that .Select(x=>)
statement must be able to be converted to a SQL expression. That is why trying to use .ToString()
(an example) can fail with the same error in some instances.
Basically, EF has no idea how to translate an object of System.Threading.Tasks.Task to a SQL statement.
Given you are trying to call this with values retrieved from the query, you will most likely need to either
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.