如何检查我的应用程序中的另一个表中是否存在数组值?

时间:2021-09-15 19:27:39

I have the following class:-

我有以下课程: -

public class ContactsDetails
    {
        public IEnumerable<AaaUserContactInfo> Info { get; set; }
    }

where AaaUserContactInfo has two foreign keys refering to other tables:-

其中AaaUserContactInfo有两个外键引用其他表: -

public partial class AaaUserContactInfo
    {
        public long USER_ID { get; set; }
        public long CONTACTINFO_ID { get; set; }
        public string desc { get; set; }

        public virtual AaaContactInfo AaaContactInfo { get; set; }
        public virtual AaaUser AaaUser { get; set; }
    }

Now i have the following class which intiate a new ContactDetails object:-

现在我有以下类来启动一个新的ContactDetails对象: -

public ActionResult CustomersDetails(long[] OrganizationIds)
{

    if (OrganizationIds == null)
    {
        return RedirectToAction("customer", new { isError = true });
    }
    else
    {

        var ContactsDetails = new ContactsDetails
        {
            Info = r.getcontactinfo(OrganizationIds)
        };
    }

        return View();    

}

now i need to return all the AaaUserContactInfo object which have their emails Ids containing part of the organization name,, something similar to:-

现在我需要返回所有AaaUserContactInfo对象,它们的电子邮件ID包含组织名称的一部分,类似于: -

public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] Organizationid)
        {
            var result = ((from uci in entities.AaaUserContactInfoes
                          join ci in entities.AaaContactInfoes on uci.CONTACTINFO_ID equals ci.CONTACTINFO_ID
                          where ci.EMAILID.ToString() == // contains any organization name in their emailIds ,, where i can get the organization name using sonthing similar to var orgname = entities.SDOrganizations.Where(a => a.ORG_ID == OrganizationIds[i]).FirstOrDefault().NAME;
                               select uci)) ;
            return result;
                                }

1 个解决方案

#1


1  

Can you try to use next query? I don't know what you want to do if FirstOrDefault() in your example return null. In my case no exception will be throws, but not records will be returned. Please note about the details of the implementation and if it worked at all.

你能尝试使用下一个查询吗?如果示例中的FirstOrDefault()返回null,我不知道你想要做什么。在我的情况下,不会抛出任何异常,但不会返回记录。请注意有关实施的详细信息以及是否有效。

public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] organizationIds)
{
    var organizationNames = entities.SDOrganizations
                           .Where(org => organizationIds.Contains(org.ORG_ID))
                           .Select(org=> org.Name);


    var result = from userContactInfo in entities.AaaUserContactInfoes
                 join contactInfo in entities.AaaContactInfoes on userContactInfo.CONTACTINFO_ID equals contactInfo.CONTACTINFO_ID

                 //check if EMAILID string has at least one organizationName as substring
                 where organizationNames.Any(orgName => contactInfo.EMAILID.ToString().Contains(orgName))
                 select userContactInfo;
    return result;
}

EDIT: updated an answer to handle many organizations selected on the basis of Organizationid. Then entities.AaaContactInfoes are selected if EMAILID is contained in name field of at least one ogranization.

编辑:更新了一个答案来处理基于Organizationid选择的许多组织。然后,如果EMAILID包含在至少一个ogranization的名称字段中,则选择entities.AaaContactInfoes。

NOTE: i have renamed some arguments used only in the method scope, just to make code more readable.

注意:我重命名了一些仅在方法范围内使用的参数,只是为了使代码更具可读性。

#1


1  

Can you try to use next query? I don't know what you want to do if FirstOrDefault() in your example return null. In my case no exception will be throws, but not records will be returned. Please note about the details of the implementation and if it worked at all.

你能尝试使用下一个查询吗?如果示例中的FirstOrDefault()返回null,我不知道你想要做什么。在我的情况下,不会抛出任何异常,但不会返回记录。请注意有关实施的详细信息以及是否有效。

public IEnumerable<AaaUserContactInfo> getcontactinfo(long[] organizationIds)
{
    var organizationNames = entities.SDOrganizations
                           .Where(org => organizationIds.Contains(org.ORG_ID))
                           .Select(org=> org.Name);


    var result = from userContactInfo in entities.AaaUserContactInfoes
                 join contactInfo in entities.AaaContactInfoes on userContactInfo.CONTACTINFO_ID equals contactInfo.CONTACTINFO_ID

                 //check if EMAILID string has at least one organizationName as substring
                 where organizationNames.Any(orgName => contactInfo.EMAILID.ToString().Contains(orgName))
                 select userContactInfo;
    return result;
}

EDIT: updated an answer to handle many organizations selected on the basis of Organizationid. Then entities.AaaContactInfoes are selected if EMAILID is contained in name field of at least one ogranization.

编辑:更新了一个答案来处理基于Organizationid选择的许多组织。然后,如果EMAILID包含在至少一个ogranization的名称字段中,则选择entities.AaaContactInfoes。

NOTE: i have renamed some arguments used only in the method scope, just to make code more readable.

注意:我重命名了一些仅在方法范围内使用的参数,只是为了使代码更具可读性。