【发布时间】:2020-04-29 06:53:22
【问题描述】:
我有两个实体 - 客户和工作。客户有 0 到多个与之关联的作业。
客户如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using JobsLedger.INTERFACES;
namespace JobsLedger.DATA.ENTITIES
{
#nullable enable
public class Client : IEntityBase, IAuditedEntityBase
{
public Client()
{
ClientNotes = new List<Note>();
Jobs = new List<Job>();
}
[Key]
public int Id { get; set; }
public string ClientNo { get; set; } = default!;
public bool Company { get; set; }
public string? CompanyName { get; set; }
public string? Abn { get; set; }
public bool IsWarrantyCompany { set; get; }
public bool RequiresPartsPayment { set; get; }
public string? ClientFirstName { get; set; }
public string ClientLastName { get; set; } = default!;
public string? Email { get; set; }
public string? MobilePhone { get; set; }
public string? Phone { get; set; }
public string? Address1 { get; set; }
public string? Address2 { get; set; }
public string? BankName { get; set; }
public string? BankBSB { get; set; }
public string? BankAccount { get; set; }
public bool Active { get; set; }
public DateTime? DateDeActivated { get; set; }
public bool Activity { get; set; }
// One warranty company client to a job.
public int? WarrantyCompanyId { get; set; }
public virtual Job? WarrantyCompany { get; set; }
// One suburb to a client.
public int? SuburbId { get; set; }
public virtual Suburb? Suburb { get; set; }
// If its a warranty company then we simply link it one to one to the brand id.
public virtual Brand? Brand { get; set; }
// Multiple notes for each client.
public virtual ICollection<Note> ClientNotes { get; set; }
// Multiple jobs for each client.
public virtual ICollection<Job> Jobs { get; set; }
public virtual ICollection<Job> WarrantyCompanyJobs { get; } = default!;
}
#nullable disable
}
工作如下:
using System.Collections.Generic;
using JobsLedger.INTERFACES;
namespace JobsLedger.DATA.ENTITIES
{
public class Job : IEntityBase, IAuditedEntityBase
{
public Job()
{
JobNotes = new List<Note>();
Visits = new List<Visit>();
}
public string? JobNo { get; set; }
public string? AgentJobNo { get; set; }
public int ClientId { get; set; } = default!;
public virtual Client Client { get; set; } = default!;
public int? BrandId { get; set; }
public virtual Brand? Brand { get; set; }
public int? TypeId { get; set; }
public virtual JobType? Type { get; set; }
public int? StatusId { get; set; }
public virtual Status? Status { get; set; }
public int? WarrantyCompanyId { get; set; }
public virtual Client? WarrantyCompany { get; set; }
public string? Model { get; set; }
public string? Serial { get; set; }
public string? ProblemDetails { get; set; }
public string? SolutionDetails { get; set; }
public virtual ICollection<Note> JobNotes { get; set; }
public virtual ICollection<Visit> Visits { get; }
public int Id { get; set; }
}
#nullable disable
}
这个 Linq Join 有效,我得到一个 ClientIndexDtos 列表。
public IQueryable<ClientIndexDto> GetClients()
{
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 = j.Count().ToString(CultureInfo.CurrentCulture)
});
return result;
}
但是..我想要每个客户的工作数量(如果有的话)...所以我问了this question on SO,有人建议这样做:
public IQueryable<ClientIndexDto> GetClients()
{
var result = this._context.Clients.GroupJoin(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 = j.Count().ToString(CultureInfo.CurrentCulture)
});
return result;
}
虽然它适用于加入版本,但在使用 groupJoin 运行时出现以下错误..
The LINQ expression 'DbSet<Client>()
.GroupJoin(
inner: DbSet<Job>(),
outerKeySelector: c => c.Id,
innerKeySelector: j => j.Id,
resultSelector: (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
}
)' 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.
我注意到 URL - https://go.microsoft.com/fwlink/?linkid=2101038 讨论了客户端 - 服务器评估...我自然希望这发生在数据库上,但我很困惑为什么一个 (Join) 可以正常工作而另一个 (GroupJoin) 却步履蹒跚。
有人可以先回答为什么它不起作用,然后告诉我我需要做什么来修复它。我会使用 Join,除非我需要知道每个客户端上存在多少工作。 GroupJoin 会给我,如果我可以让它工作......
我知道我希望它在数据库端执行,即获取一个 IQueryable 等,这样它就不会将更多的记录拖回客户端。
任何帮助表示赞赏。
【问题讨论】:
标签: c# linq linq-to-sql entity-framework-core