简体   繁体   English

Group By then Select 内部元件 Linq asp.net 核心3

[英]Group By then Select inner elements Linq asp.net Core 3

before every thing i tried a lot of answers and fixes but didnt work for me, the problem is im trying to get a list of this Dto:在每件事之前我尝试了很多答案和修复但对我没有用,问题是我试图获取这个 Dto 的列表:

        public string Ministry { get; set; }
        public string Department { get; set; }
        public string EmployeeCount { get; set; }
        public List<string> Accounts { get; set; }

using this linq query:使用此 linq 查询:

var result = await (from emp in _db.Employees
                                join c in _db.EmployeeBlackList
                                on emp.AccountNo equals c.AccountNo into joined
                                from c in joined.DefaultIfEmpty()
                                group emp by new { emp.Ministry, emp.Department }
                                    into groups
                                select new MinstryDepartmentEmployeeCount()
                                {
                                    Ministry = groups.Key.Ministry,
                                    Department = groups.Key.Department,
                                    EmployeeCount = groups.Count().ToString(),
                                    Accounts = groups.Select(x => x.AccountNo).ToList()
                                }).ToListAsync();

the error exception i am getting is this:我得到的错误异常是这样的:

One or more errors occurred.发生一个或多个错误。 (The LINQ expression 'Select<Employee, string>(\r\n source: GroupByShaperExpression:\r\n KeySelector: new { \r\n Ministry = e.Ministry, \r\n Department = e.Department\r\n }, \r\n ElementSelector:EntityShaperExpression: \r\n EntityType: Employee\r\n ValueBufferExpression: \r\n ProjectionBindingExpression: EmptyProjectionMember\r\n IsNullable: False\r\n, \r\n selector: (x) => x.AccountNo)' 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.) (LINQ 表达式 'Select<Employee, string>(\r\n 来源:GroupByShaperExpression:\r\n KeySelector: new { \r\n Ministry = e.Ministry, \r\n Department = e.Department\r\ n }, \r\n ElementSelector:EntityShaperExpression: \r\n EntityType: Employee\r\n ValueBufferExpression: \r\n ProjectionBindingExpression: EmptyProjectionMember\r\n IsNullable: False\r\n, \r\n 选择器: ( x) => x.AccountNo)' 无法翻译。要么以可翻译的形式重写查询,要么通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 的调用显式切换到客户端评估,或 ToListAsync()。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 。)

i already know that this statement is generating the error:我已经知道这个语句正在产生错误:

Accounts = groups.Select(x => x.AccountNo).ToList()

i can not take all info in the employee table because it has more than 2 million rows and more than 7 columns.我无法获取员工表中的所有信息,因为它有超过 200 万行和超过 7 列。

the main objective of this query is to convert this:此查询的主要目标是转换它:

Ministry    Department   AccountNo
A           AA           1111
A           AA           2222
B           BB           3333
B           BB           4444

into this:进入这个:

[
  {
    Ministry: 'A' ,
    Department: 'AA' ,
    EmployeeCount: 2 ,
    Accounts:[
               {
                 '1111'  
               },
               {
                 '2222'  
               },
             ]
  },
  {
    Ministry: 'B' ,
    Department: 'BB' ,
    EmployeeCount: 2 ,
    Accounts:[
               {
                 '3333'  
               },
               {
                 '4444'  
               },
             ]
  }
]

thanks in advance.提前致谢。

Update: this question wants the same as i want, i have used the same answer for it, but i still get the same error, so i believe that the reason is the migration of asp.net core to 3.o更新:这个问题和我想要的一样,我使用了相同的答案,但我仍然得到相同的错误,所以我相信原因是 asp.net 内核迁移到 3.o
questionLink 问题链接

While this might be useful, I ran into the same situation for myself.虽然这可能有用,但我自己也遇到了同样的情况。 I ended up converting the query into a View and then using我最终将查询转换为视图,然后使用

https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations

this method of creating the projection I needed, as for this being EFCore version is dependent on how you create the Keyless type in your datacontext.这种创建我需要的投影的方法,因为这是 EFCore 版本,取决于您如何在数据上下文中创建 Keyless 类型。

What I really gained from this was a ridiculous amount of speed in return, the query normally took 8-18 seconds now it returns the exact data in a fraction of that.我真正从中获得的是一个荒谬的速度回报,查询通常需要 8-18 秒,现在它返回的确切数据只是其中的一小部分。 Moving the query away from EFC doing the work to the server doing the work, only bottle neck would be the wire at that point.将查询从执行工作的 EFC 移至执行工作的服务器,此时只有瓶颈是线路。

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

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