私が使用している次のクエリがあります(これは主にここで受け取ったヘルプからのものです):
public IEnumerable<Entities.AuditAgency> GetAuditRuleAgencyRecords(IEnumerable<Entities.AuditRuleEnterprise> rules)
{
using (LinqModelDataContext db = new LinqModelDataContext())
{
// Left-Outer Joins on Agency and its various other tables.
var auditAgencyRecords = (from ag in db.Agencies
join ara in db.AuditRuleAccounts on ag.Agency_Id equals ara.AgencyID into aran
from ara in aran.DefaultIfEmpty()
join arr in db.AuditRuleResults on ara.AuditRuleAccountID equals arr.AuditRuleAccountID into arrn
from arr in arrn.DefaultIfEmpty()
join are in db.AuditRuleEnterprises on arr.AuditRuleEnterpriseID equals are.AuditRuleEnterpriseID into aren
from are in aren.DefaultIfEmpty()
select new
{
AgencyID = ag.Agency_Id,
AgencyName = ag.Agency_Name,
AuditRuleEnterpriseID = arr.AuditRuleEnterpriseID,
AuditRuleEnterpriseName = are.OverrideDisplayName,
CorrectedDate = arr.CorrectedDate,
NbrDaysToCorrect = arr.NbrDaysToCorrect,
});
IEnumerable<AuditAgency> AuditAgencies = auditAgencyRecords
.GroupBy(a => a.AgencyID)
.Select(ag => new AuditAgency()
{
AgencyID = ag.Key,
AgencyName = ag.First().AgencyName,
Rules = ag
.GroupBy(agr => agr.AuditRuleEnterpriseID)
// ----> Do a left outer join on parameter "rules" object and the returned group above
// ----> on both of their ID's
.Select(agrg => new AuditAgencyRule() // Now I would like to only be creating "rules" for the rules with IDs that match the rules passed into this method
{
AuditRuleID = agrg.Key,
AuditRuleName = agrg.First().AuditRuleEnterpriseName,
Days = (Int32)agrg.Average(agrgr => agrgr.NbrDaysToCorrect)
})
}).ToList();
return AuditAgencies;
}
これにより、AuditAgency オブジェクトのリストが返され、各 AuditAgency には AuditAgencyRules のリストが含まれます。
さて、このクエリの最後のステップとして、私が問題を抱えている部分.. IEnumerable ルールがパラメーターとしてこのメソッドに渡されていることがわかります。
私がやりたいのは、そこにある 2 番目のクエリで、各エージェンシーのルールのリストを作成することです。ローカルの「ルール」オブジェクトで左外部結合を実行したいと考えています。渡す各ルール オブジェクトには、rule.ID があります。各エージェンシーには、渡されたルールのみを含め、それらのデータがない場合は、コンテンツを null のままにしておきます。
現在、私のクエリには、データベースから返されたすべてのルールとそのデータが含まれています。ただし、データベースから返されるルールと一致するかどうかに関係なく、メソッドに渡されるルールのみを含める必要があります。つまり、ローカルの「ルール」オブジェクトに対して、現在そこにあるルールを使用して左外部結合を行う必要があります。
上記のコードのどこにコメントを追加したかを確認して、どこで何をしようとしているのかを説明してください。
そのため、「rules」に ID が設定された 3 つのルールのみが含まれている場合、このクエリは、それらのルールのデータがあるかどうかに関係なく、各機関の 3 つのルールのみを返します (そのデータは null になります)。各エージェンシーのすべてのルールを返すのではなく、それが現在行われていることです。
LINQ to SQL クエリで "ローカル" オブジェクトに対してこの左外部結合を記述するにはどうすればよいですか?
上記の 2 番目のクエリの後で、「ルール」オブジェクトと AuditAgencies のルールに対して left-outer-join を実行する試みを次に示します。
foreach (var agency in AuditAgencies)
{
agency.Rules = from rule in rules
join lr in agency.Rules on rule.EnterpriseID equals lr.AuditRuleID into g
from lr in g.DefaultIfEmpty()
select new AuditAgencyRule
{
AuditRuleID = rule.EnterpriseID,
AuditRuleName = rule.Name,
Days = lr.Days,
Flagged = lr.Flagged,
PercentFlagged = lr.PercentFlagged
};
}
サイコロはありません。今、すべてのルールの代わりに、~no~ ルールを取得しています。私が望むのは、このメソッドに渡すルールを取得することだけです。
誰かが私を正しい方向に向けることができますか?