0

我有以下课程:-

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

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

现在我有以下类启动一个新的 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();    

}

现在我需要返回所有的 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;
                                }
4

1 回答 1

1

您可以尝试使用下一个查询吗?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;
}

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

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

于 2013-01-04T00:27:22.367 回答