本文介绍了C#Linq将两个实体转换为DTO,但提供了其中一个实体的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

        var result = this._context.Clients.Join(this._context.Jobs, c => c.Id, j => j.Id, (c, j) =>
            new ClientIndexDto
            {
                Id = c.Id,
                ClientNo = c.ClientNo,
                Active = c.Active,
                ClientFirstName = c.ClientFirstName,
                ClientLastName = c.ClientLastName,
                Company = c.Company,
                CompanyName = c.CompanyName,
                MobilePhone = c.MobilePhone,
                IsWarrantyCompany = c.IsWarrantyCompany,
                JobsCount = ???

            });

我想获得客户的工作数量..有办法吗?

I would like to get the number of jobs a client has.. is there a way to do this?

推荐答案

您也可以尝试以下代码:

You can also try this code:

 var result = (from b in _context.Clients.ToList()
                      join a in _context.Jobs.ToList() on b.Id equals a.Id
                      group a by b into g
                      select new ClientIndexDto
                      {
                          Id = g.Key.Id,
                          ClientNo = g.Key.ClientNo,
                          Active = g.Key.Active,
                          ClientFirstName = g.Key.ClientFirstName,
                          ClientLastName = g.Key.ClientLastName,
                          Company = g.Key.Company,
                          CompanyName = g.Key.CompanyName,
                          MobilePhone = g.Key.MobilePhone,
                          IsWarrantyCompany = g.Key.IsWarrantyCompany,
                          JobsCount = g.Count()
                      });

这篇关于C#Linq将两个实体转换为DTO,但提供了其中一个实体的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:25