views:

21

answers:

1

I have the following entities:

Clients
-- ClientID
-- ClientName

Contractor
-- ContractorID
-- ContractorName

PreferredContractors
-- PreferredContractorID
-- ClientID
-- ContractorID

So I have a list of clients and contractors. The clients prefer to work with certain contractors than the others. I want to build a LINQ to Entity query which pulls all the contractors with a boolean field indicating whether the contractor is preferred or not.

 public IQueryable<PreferredContractor> GetPreferredContractors(int clientID)
    {
        var preferredContractors = from c in db.Contractors
                  from pc in db.PreferredContractors.DefaultIfEmpty()
                  select new PreferredContractor
                             {
                                 ContractorID = c.ContractorID,
                                 ContractorName = c.ContractorName,
                                 IsPreferred = // This is where I need help
                             };

        return preferredContractors;
    }

How can I determine if the contractor is preferred or not?

+1  A: 
    var preferredContractors =
              from c in db.Contractors
              join pc in db.PreferredContractors.Where(pc2 => pc2.ClientId == clientId) on c.ContractorId equals pc.ContractorId into j
              from pc in j.DefaultIfEmpty()
              select new PreferredContractor
                         {
                             ContractorID = c.ContractorID,
                             ContractorName = c.ContractorName,
                             IsPreferred = pc != null
                         };
Thomas Levesque
Thanks for the prompt response. But where should I use the clientID? I need to show the preferred contractors for a selected client.
Kumar
@Kumar, I updated the code in my answer
Thomas Levesque
Thanks. It Works!!!!
Kumar