How to write this Link2Sql select query

Posted by RememberME on Stack Overflow See other posts from Stack Overflow or by RememberME
Published on 2010-05-25T18:12:08Z Indexed on 2010/05/25 18:51 UTC
Read the original article Hit count: 344

Filed under:
|
|

I have 3 tables: subcontracts, companies and contacts. Each table has a active_status flags which shows that the item is still active (hasn't been deleted).
Each contact has a company_id field which stores the id of the contact's company.
Each subcontract has a company_id field which stores the subcontract's company.
Each company has a company_id field which holds its guid and a primary_company field, b/c the company could be a subsidiary. If it's a subsidiary, the primary_company field holds the id of the primary company.

I have a subcontract form. On the form, I have a drop-down of contacts which are stored in a contact table. Currently the drop-down lists all contacts. I would like to have the drop-down only list contacts which belong to the subcontract's company or any of that company's subsidiaries.

I have the following query which I use elsewhere in the program to pass a company_id and get its subsidiaries.

public IQueryable<company> GetSubsidiaryCompanies(Guid id)
            {
                return
                    from c in db.companies
                    where c.primary_company == id && c.active_status == true
                    select c;
            }

This is my current contact selection

public IQueryable<contact> GetContacts()
    {
        return
            from c in db.contacts
            where c.active_status == true
            orderby c.contact_name
            select c;
    }

What I need to do is pass it the subcontract.company_id and then only return the contacts where contact.company_id == subcontract.company_id or contact.company_id == one of the subsidiary ids.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql