この Linq ステートメントは、あなたが望むことを行います:
var rules = from r in db.CategorySelectionRules
where r.CategorySelectionRuleDetails.Count() == selectedCategories.Count &&
r.CategorySelectionRuleDetails.Where(cr => selectedCategories.Contains(cr.CategoryId.Value)).Count() == selectedCategories.Count
select r;
同じカテゴリ要件を持つ 2 つの異なるルールがある場合、これは複数のルールも返します。
プログラム全体:
データベース構造の生成に使用される SQL
CREATE TABLE EntryCategory (
CategoryId INTEGER IDENTITY PRIMARY KEY,
CategoryDescription VARCHAR(100)
)
CREATE TABLE CategorySelectionRules (
RuleId INTEGER IDENTITY PRIMARY KEY,
RuleDescription VARCHAR(100),
RuleMessage VARCHAR(255),
Discount DECIMAL
)
CREATE TABLE CategorySelectionRuleDetail (
RuleCategoryId INTEGER IDENTITY PRIMARY KEY,
RuleId INTEGER REFERENCES CategorySelectionRules(RuleId),
CategoryId INTEGER REFERENCES EntryCategory(CategoryId)
)
C# プログラム (コンソール アプリ)
namespace StackOverFlowTesting
{
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
//Load Data
using (var db = new DatabaseDataContext())
{
//Check to see if the data has already been loaded
if (db.EntryCategories.Count() == 0)
{
//Load in inital categories
db.EntryCategories.InsertOnSubmit(new EntryCategory() { CategoryDescription = "Category1" });
db.EntryCategories.InsertOnSubmit(new EntryCategory() { CategoryDescription = "Category2" });
db.EntryCategories.InsertOnSubmit(new EntryCategory() { CategoryDescription = "Category3" });
//Load in inital rules
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule1", RuleMessage = "Rule1 Message", Discount = .5M });
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule2", RuleMessage = "Rule2 Message", Discount = .5M });
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule3", RuleMessage = "Rule3 Message", Discount = .5M });
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule4", RuleMessage = "Rule4 Message", Discount = .5M });
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule5", RuleMessage = "Rule5 Message", Discount = .5M });
db.CategorySelectionRules.InsertOnSubmit(new CategorySelectionRule() { RuleDescription = "Rule6", RuleMessage = "Rule6 Message", Discount = .5M });
//Link up the categories and rules
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 1, RuleId = 1 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 1, RuleId = 2 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 2, RuleId = 2 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 1, RuleId = 3 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 2, RuleId = 3 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 3, RuleId = 3 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 1, RuleId = 4 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 3, RuleId = 4 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 2, RuleId = 5 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 3, RuleId = 5 });
db.CategorySelectionRuleDetails.InsertOnSubmit(new CategorySelectionRuleDetail() { CategoryId = 3, RuleId = 6 });
//Populate the database with our temp records
db.SubmitChanges();
}
/** Different ways of testing **/
//var selectedCategories = new List<int>() { 1 }; //Rule 1
var selectedCategories = new List<int>() { 1, 2 }; //Rule 2
//var selectedCategories = new List<int>() { 1, 2,3 }; //Rule 3
//var selectedCategories = new List<int>() { 1, 3 }; //Rule 4
//var selectedCategories = new List<int>() { 2, 3 }; //Rule 5
//var selectedCategories = new List<int>() { 3 }; //Rule 6
var rules = from r in db.CategorySelectionRules
where r.CategorySelectionRuleDetails.Count() == selectedCategories.Count &&
r.CategorySelectionRuleDetails.Where(cr => selectedCategories.Contains(cr.CategoryId.Value)).Count() == selectedCategories.Count
select r;
if (rules.Count() == 0)
{
Console.WriteLine("No rule found");
}
else
{
Console.WriteLine(rules.Count().ToString() + " Rules Found");
foreach (var rule in rules)
{
Console.WriteLine(rule.RuleDescription);
}
}
Console.ReadLine();
}
}
}
}