简体   繁体   English

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

[英]How to check if array values exist in another table inside my application?

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

Can you try to use next query? 您可以尝试使用下一个查询吗? I don't know what you want to do if FirstOrDefault() in your example return null. 如果您的示例中的FirstOrDefault()返回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. 请注意实施的细节以及它是否可行。

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 . 编辑:更新了一个答案,以处理根据Organizationid选择的许多组织。 Then entities.AaaContactInfoes are selected if EMAILID is contained in name field of at least one ogranization. 然后entities.AaaContactInfoes如果选择EMAILID被包含在至少一个ogranization的名称字段。

NOTE: i have renamed some arguments used only in the method scope, just to make code more readable. 注意:我重命名了一些仅在方法范围内使用的参数,只是为了使代码更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查我的数组中是否有重复的值? - How do I check if my array has repeated values inside it? 如何检查数组的每个项目是否在mvc 4中的另一个数组中存在? - How to check whether each item of an array exist in another array in mvc 4? 如何检查并杀死我的应用程序的另一个实例? - how can I check and kill another instance of my application? 如何在ASP.NET MVC应用程序中首先使用EF代码检查SQL表中是否存在数据? - How to check if data exist in SQL table using EF code first in ASP.NET MVC application? 检查一个数组是否包含另一个数组的值 - check if an array contains values of another array 如何检查表中是否存在该名称 - how can i check if the name exist in table 如何检查表中是否存在值,如果存在则删除它? - How to Check if a value exist in a Table,If it exists then Delete it? 如何检查,我从文本框中输入的“ ID”在MySql表中不存在? - How to check, that “ID” which i put from textbox does not exist in my MySql table? 如何检查表的十进制字段中存在的十进制数组中的任何值? - How to check any value from Decimal array exist in a decimal field of a table? 检查表中是否存在单词 - check if word exist in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM