简体   繁体   中英

System.InvalidOperationException: The LINQ expression

I set up an asynchronous method in webapi(.NET Core 3.1), use linq to search the database and get the number of each category, and return it in the controller. I use Swagger to test but there are always errors. I don't know where the error is. Can i ask for some help?

The service:

public async Task<ClassficationSimpleInfo[]> SearchZulib(string token, string keyWord)  
{  
    var data = zudb.ZuFileinfo.Include(x => x.Classify).Where(x => x.IsHiden != 1)  
        .Where(x => keyWord == "" || x.FamilyName.Contains(keyWord) || x.Description.Contains(keyWord))  
        .GroupBy(x => x.Classify)  
        .Select(x => new { classify = x.Key, count = x.Count() })  
        .ToList();  
    var result = data.Select(x => new ClassficationSimpleInfo(x.classify.Name, x.classify.ClassificationCode)  
    {  
        Count = x.count,  
        Folder = x.classify.Folder,  
        
    }).ToArray();  
    return result;  
}

The controller:

        [HttpGet]
        [Route("Controller/SearchZulib")]
        public async Task<ClassficationSimpleInfo[]> SearchZulib(string token, string keyWord)
        {
            return await service.SearchZulib(token, keyWord);
        }  

The definition of related Class:

namespace ZulibWebServer.Entities
{
    public class ClassficationSimpleInfo
    {
        public int Id { get; set; }

        public string ClassifyCode { get; set; }

        public string Name { get; set; }

        public int Count { get; set; }

        public string Folder { get; set; }

        public bool Existed { get; set; }

        public ClassficationSimpleInfo(string name, string classifyCode)
        {
            Name = name;
            ClassifyCode = classifyCode;
        }
    }
}
namespace ZulibWebServer.Models
{
    public partial class ZuFileinfo
    {
        public int FileId { get; set; }
        public string FamilyName { get; set; }
        public string FileUrl { get; set; }
        public int ClassifyId { get; set; }
        public string Description { get; set; }
        public byte[] ThumbImage { get; set; }
        public int? MinVer { get; set; }
        public string LargeImage { get; set; }
        public int IsHiden { get; set; }
        public string UploaderName { get; set; }
        public int? UploaderId { get; set; }

        public virtual ZuClassfication Classify { get; set; }
    }
}
public partial class ZuClassfication
    {
        public ZuClassfication()
        {
            ZuFileinfo = new HashSet<ZuFileinfo>();
            ZuMapingrule = new HashSet<ZuMapingrule>();
        }

        public int ClassificationIdid { get; set; }
        public string ClassifyName { get; set; }
        public string ClassificationCode { get; set; }
        public string RelQc { get; set; }
        public string RelCbimcode { get; set; }
        public string RelOminClass { get; set; }
        public string Reluniformat { get; set; }
        public string OtherCode { get; set; }
        public string Name { get; set; }
        public int? ParentCodeId { get; set; }
        public string Folder { get; set; }

        public virtual ICollection<ZuFileinfo> ZuFileinfo { get; set; }
        public virtual ICollection<ZuMapingrule> ZuMapingrule { get; set; }
    }
}

But the error response is

System.InvalidOperationException: The LINQ expression 'DbSet
.Where(z => z.IsHiden != 1) .Where(z => False || z.FamilyName.Contains(__keyWord_0) || z.Description.Contains(__keyWord_0))
.Join( outer: DbSet, inner: z => EF.Property>(z, "ClassifyId"), outerKeySelector: z0 => EF.Property>(z0, "ClassificationIdid"), innerKeySelector: (o, i) => new TransparentIdentifier( Outer = o, Inner = i )) .GroupBy( source: z => z.Inner, keySelector: z => z.Outer)' could not be translated.
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

I tested it again and found the error in GroupBy(x => x.Classify), so i modified the code to query the database twice.

var data =await zudb.ZuFileinfo
    .Where(x => x.IsHiden != 1)
    .Where(x => keyWord == "" || x.FamilyName.Contains(keyWord) || x.Description.Contains(keyWord))
    .GroupBy(x => x.ClassifyId).Select(x => new { classifyId = x.Key, count = x.Count() })
    .ToListAsync();

var classifies =await zudb.ZuClassfication.ToDictionaryAsync(x => x.ClassificationIdid);

var result = data.Select(x =>
    {
         if (!classifies.TryGetValue(x.classifyId, out var classify)) return null;
         return new ClassficationSimpleInfo(classify.Name, classify.ClassificationCode)
              {
                  Count = x.count,
                  Folder = classify.Folder,
              };
    }).ToArray();

Finally, I succeeded.

I tested it again and found the error in GroupBy(x => x.Classify)

Yeah, it is invalid to GroupBy a Navigation property.

Also, you can simplify your query by linq like below:

var data = zudb.ZuFileinfo.Include(x => x.Classify).Where(x => x.IsHiden != 1)
    .Where(x => keyWord == "" || x.FamilyName.Contains(keyWord) || x.Description.Contains(keyWord))
    .GroupBy(x => x.ClassifyId)
    .Select(x => new { classifyId = x.Key, count = x.Count() })
    .ToList();

var result = (from d in data
            join c in zudb.ZuClassfication on d.classifyId equals c.ClassificationIdid
            select new ClassficationSimpleInfo(c.Name, c.ClassificationCode)
            {
                Count = d.count,
                Folder = c.Folder
            }).ToArray();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM