Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查两个字段组合中的唯一性_C# - Fatal编程技术网

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

C# 检查两个字段组合中的唯一性,c#,C#,我有表clientcontactcompany表,并检查是否存在电子邮件地址和clientcompanyId的组合。我在这两个字段上都设置了唯一的约束。我正在尝试在我的存储库层中编写逻辑 客户公司联系人类别 public partial class ClientCompanyContact { public ClientCompanyContact() { FxforwardTrade = new HashSet<FxforwardTrade>();

我有表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部分类ClientCompanyContact
{
公共客户公司联系人()
{
FxforwardTrade=newhashset();
Fxoption=newhashset();
}
公共int Id{get;set;}
public int ClientCompanyId{get;set;}
公共字符串标题{get;set;}
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共字符串电子邮件{get;set;}
公共字符串直接{get;set;}
公共字符串移动电话{get;set;}
公共字符串{get;set;}
公共日期时间?生日{get;set;}
公共bool授权{get;set;}
公共字节[]UpdateTimeStamp{get;set;}
public int UpdatedByAuthUserId{get;set;}
公共日期时间UpdatedDateTime{get;set;}
公共字符串注释{get;set;}
公共字符串全名{get;set;}
公共布尔值{get;set;}
公共bool RecAmreport{get;set;}
public int?AuthUserId{get;set;}
公共字符串位置{get;set;}
公共布尔?PrimaryContact{get;set;}
公共bool RecActivityReport{get;set;}
公共布尔被删除{get;set;}
公共字符串Aspnumber{get;set;}
公共日期时间?AspcreationDate{get;set;}
公共日期时间?LastTelephoneChangeDate{get;set;}
公共日期时间?LastEmailChangeDate{get;set;}
公共字符串BloombergPi{get;set;}
公共字符串NiNumber{get;set;}
公共AuthUser AuthUser{get;set;}
public ClientCompany ClientCompany{get;set;}
公共AuthUser UpdatedByAuthUser{get;set;}
公共ICollection FxforwardTrade{get;set;}
公共ICollection选项{get;set;}
}
客户联系人存储库

public IGenericRepo<ClientCompanyContact> ClientCompanyContactRepository =>
        _clientCompanyContactRepository = _clientCompanyContactRepository ?? new GenericRepo<ClientCompanyContact>(Context);
public-IGenericRepo-ClientCompanyContactRepository=>
_clientCompanyContactRepository=\u clientCompanyContactRepository??新的通用报告(上下文);
这是我试过的,但没有编译。做这件事的权利是什么

   public async Task<bool> UniqueEmail(string email, string ClientCompanyId)
    {
        return await ClientCompanyContactRepository.Get().Where(x => x.Email == email && x.ClientCompanyId = ClientCompanyId);
    }
public异步任务UniqueEmail(字符串email,字符串ClientCompanyId)
{

返回await ClientCompanyContactRepository.Get(),其中(x=>x.Email==Email&&x.ClientCompanyId=ClientCompanyId); }
首先,将ClientCompanyID作为int传递进来,然后缺少一个等号,应该是:

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;
}

请注意,我们不知道
Get()


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

您的代码中几乎没有致命的小错误

方法应该更像这样

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;
}

请注意,我们不知道
Get()


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

是否为ClientCompanyContactRepository.Get()。正确位置?另一件事是整个表达式的结果应该是true或false
x.ClientCompanyId
是一个
int
,而
ClientCompanyId
是一个
字符串。你确定这是正确的吗?它是否至少应该是
==int.Parse(ClientCompanyId)
?返回wait wait ClientCompanyContactRepository.Get()。其中(x=>x.Email==Email&&x.ClientCompanyId==ClientCompanyId);ClientcompanyId为和int。我的错误我在执行上述操作时遇到以下错误。IEnumerable不包含GetAwaiter的定义,并且在ClientCompanyContactRepository.Get()中找不到接受IEnumerable类型的第一个参数的可访问方法GetAwaiter。正确的位置是?另一件事是整个表达式的结果应该是true或false
x.ClientCompanyId
是一个
int
,而
ClientCompanyId
是一个
字符串。你确定这是正确的吗?它是否至少应该是
==int.Parse(ClientCompanyId)
?返回wait wait ClientCompanyContactRepository.Get()。其中(x=>x.Email==Email&&x.ClientCompanyId==ClientCompanyId);ClientcompanyId为和int。我的错误我在执行上述操作时遇到以下错误。IEnumerable不包含GetAwaiter的定义,并且找不到接受IEnumerable类型的第一个参数的可访问方法GetAwaiter。ClientCompanyId=ClientCompanyId
是一个赋值,而不是比较。赋值返回赋值结果,它是
int
,而不是
bool
,这就是它导致错误的原因。此外,您将参数
ClientCompanyId
作为
字符串
传入,但它在类中似乎是
int
,因此,您也需要首先对字符串进行转换。如果您尝试的方法不起作用,您应该发布所得到的错误。
x.ClientCompanyId=ClientCompanyId1
是一个赋值,而不是一个比较。赋值返回赋值结果,它是
int
,而不是
bool
,这就是它导致错误的原因。此外,您将参数
ClientCompanyId
作为
字符串
传入,但它在类中似乎是
int
,因此,您也需要首先对字符串进行转换。您应该发布错误