我有以下实体:
客户
- ClientID
- ClientName
承包商
- 承包商ID
- ContractorName
PreferredContractors
- PreferredContractorID
- ClientID
- 承包商ID
所以我有一份客户和承包商名单。客户更愿意与某些承包商合作。我想构建一个LINQ to Entity查询,该查询使用一个布尔字段来提取所有承包商,指示承包商是否是首选。
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;
}
如何判断承包商是否优先?
答案 0 :(得分:3)
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
};