简体   繁体   中英

How can I write the “Where” clause in the following LINQ to SQL Query?

I'm working on the following LINQ query:

public void GetAuditRuleAgencyRecords(IEnumerable<Entities.AuditRule> rules)
{
    using (LinqModelDataContext db = new LinqModelDataContext())
    {
        var auditAgencyRecords = (from ag in db.Agencies
                        join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID
                        join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID
                        join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID
                        select new
                        {

                            AgencyID = ag.Agency_Id,
                            AgencyName = ag.Agency_Name,
                            AuditRuleEnterpriseID = arr.AuditRuleEnterpriseID,
                            CorrectedDate = arr.CorrectedDate,
                            NbrDaysToCorrect = arr.NbrDaysToCorrect,      

                        }).ToList();
    }
}

You can see that I'm passing in an IEnumerable rules. Each AuditRule object that I pass in has a property called "ID".

What would my where clause look like for this query if I want to say, only return the records where the table column AuditRuleEnterprise.AuditID matches any one of the ID's in my rules "ID" property (the objects I've passed into the method)?

Try:

.Where(rules.Select(r => r.ID).Contains(arr.AuditRuleEnterpriseID.AuditID))

or, in query syntax

where rules.Select(r => r.ID).Contains(arr.AuditRuleEnterpriseID.AuditID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM