简体   繁体   English

System.InvalidOperationException:LINQ 表达式

[英]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.我在webapi(.NET Core 3.1)中设置了一个异步方法,使用linq搜索数据库并获取每个类别的数量,并在控制器中返回。 I use Swagger to test but there are always errors.我使用 Swagger 来测试,但总是有错误。 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 System.InvalidOperationException: LINQ 表达式 'DbSet
.Where(z => z.IsHiden != 1) .Where(z => False || z.FamilyName.Contains(__keyWord_0) || z.Description.Contains(__keyWord_0)) .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. .Join(外层:DbSet,内层: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)' 无法翻译。
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().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

I tested it again and found the error in GroupBy(x => x.Classify), so i modified the code to query the database twice.再次测试,发现GroupBy(x => x.Classify)错误,于是修改代码两次查询数据库。

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)我再次测试,发现 GroupBy(x => x.Classify) 中的错误

Yeah, it is invalid to GroupBy a Navigation property.是的,它对 GroupBy 导航属性无效。

Also, you can simplify your query by linq like below:此外,您可以通过 linq 简化您的查询,如下所示:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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