检查两个字段组合中的唯一性



我有一个表clientcontactcompany表,并检查电子邮件地址和clientcompanyId的组合是否存在。我在这两个字段上都设置了唯一的约束。我正在尝试在我的存储库层中编写逻辑

客户公司联系类别

public partial class ClientCompanyContact
{
public ClientCompanyContact()
{
FxforwardTrade = new HashSet<FxforwardTrade>();
Fxoption = new HashSet<Fxoption>();
}
public int Id { get; set; }
public int ClientCompanyId { get; set; }
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string TelephoneDirect { get; set; }
public string TelephoneMobile { get; set; }
public string TelephoneOther { get; set; }
public DateTime? Birthday { get; set; }
public bool Authorized { get; set; }
public byte[] UpdateTimeStamp { get; set; }
public int UpdatedByAuthUserId { get; set; }
public DateTime UpdatedDateTime { get; set; }
public string Notes { get; set; }
public string Fullname { get; set; }
public bool RecNotifications { get; set; }
public bool RecAmreport { get; set; }
public int? AuthUserId { get; set; }
public string Position { get; set; }
public bool? PrimaryContact { get; set; }
public bool RecActivityReport { get; set; }
public bool IsDeleted { get; set; }
public string Aspnumber { get; set; }
public DateTime? AspcreationDate { get; set; }
public DateTime? LastTelephoneChangeDate { get; set; }
public DateTime? LastEmailChangeDate { get; set; }
public string BloombergGpi { get; set; }
public string NiNumber { get; set; }
public AuthUser AuthUser { get; set; }
public ClientCompany ClientCompany { get; set; }
public AuthUser UpdatedByAuthUser { get; set; }
public ICollection<FxforwardTrade> FxforwardTrade { get; set; }
public ICollection<Fxoption> Fxoption { get; set; }
}

客户联系存储库

public IGenericRepo<ClientCompanyContact> ClientCompanyContactRepository =>
_clientCompanyContactRepository = _clientCompanyContactRepository ?? new GenericRepo<ClientCompanyContact>(Context);

这是我尝试不编译的内容。做有什么权利

public async Task<bool> UniqueEmail(string email, string ClientCompanyId)
{
return await ClientCompanyContactRepository.Get().Where(x => x.Email == email && x.ClientCompanyId = ClientCompanyId);
}

首先,将ClientCompanyID作为int传入,然后您缺少一个等号,应该是:

x => x.Email == email && x.ClientCompanyId == ClientCompanyId

您的代码中很少有小但致命的错误。

方法应该更像这样。

public async Task<bool> UniqueEmail(string email, int clientCompanyId)
{
var count = await ClientCompanyContactRepository.Get()
.Count(x => x.Email == email && x.ClientCompanyId == clientCompanyId);
return count < 2;
}

请注意,.Count(x => ...)可以是.Where( x => ...).Count()

更改:

  1. clientCompanyId作为int传递,因此'int ClientCompanyId'
  2. 您需要返回一个bool而不是一个列表对象,所以"bool anyMatching = x.Any(...); return !anyMatching">
  3. 参数名称应以公共字母开头,因此为"int clientCompanyId">
  4. 比较使用==,而不是=,因此使用"== ClientCompanyId">
  5. 如果Getasync,那么它应该是GetAsync,或者甚至是GetAllAsync

更新:Get似乎不是异步的。让我们放弃所有异步部分。

public bool UniqueEmail(string email, int clientCompanyId)
{
var count = ClientCompanyContactRepository.Get()
.Count(x => x.Email == email && x.ClientCompanyId == clientCompanyId);
return count < 2;
}

请注意,我们不知道Get()返回了什么,所以我们不知道过滤是在内存中还是在数据库中进行。

我怀疑Get()可能会返回所有记录,因此过滤将在内存中进行。您可能需要将UniqueEmail移动到存储库中。*

最新更新