public class DiscountRule
public bool CategoryInclude
public bool CheckDiscountRuleClash(DiscountRule other)
if (this.CategoryId == other.CategoryId && this.BrandId == other.BrandId)
if (this.BrandInclude && this.CategoryInclude && !other.BrandInclude && !other.CategoryInclude)
else if (!this.BrandInclude && !this.CategoryInclude && other.BrandInclude && other.CategoryInclude)
else if (this.BrandInclude && !this.CategoryInclude && !other.BrandInclude && other.CategoryInclude)
else if (!this.BrandInclude && this.CategoryInclude && other.BrandInclude && !other.CategoryInclude)
this.RuleClashes = false;
public static void Main()
from categoryId in Enumerable.Range(0, 80)from brandId in Enumerable.Range(0, 80)select new DiscountRule
BrandId = brandId, CategoryId = categoryId, BrandInclude = true, CategoryInclude = true
rules.Add(new DiscountRule
var sw1 = System.Diagnostics.Stopwatch.StartNew();
rule.BrandId, rule.CategoryId
i.BrandInclude, i.CategoryInclude
foreach (var idGroup in groups.Where(g => g.clashGroups.Count() > 1))
foreach (var clashGroup in idGroup.clashGroups)
foreach (var rule in clashGroup)
Console.WriteLine("CategoryId: {0}, BrandId: {1}, CategoryInclude: {2}, BrandInclude: {3}", rule.CategoryId, rule.BrandId, rule.CategoryInclude, rule.BrandInclude);
Console.WriteLine("Fast method");
Console.WriteLine(sw1.Elapsed);
var sw2 = System.Diagnostics.Stopwatch.StartNew();
var conflictingRules = rules.SelectMany(rule =>
rules.Where(other => rule.CheckDiscountRuleClash(other))
.Select(other=> new { rule, other }));
foreach(var conflict in conflictingRules)
Console.WriteLine("CategoryId: {0}, BrandId: {1}, CategoryInclude: {2}, BrandInclude: {3}", conflict.rule.CategoryId, conflict.rule.BrandId, conflict.rule.CategoryInclude, conflict.rule.BrandInclude);
Console.WriteLine("Stupid method");
Console.WriteLine(sw2.Elapsed);
Console.WriteLine("Good one outperformed stupid one {0} times", (double)sw2.ElapsedMilliseconds / (double)sw2.ElapsedMilliseconds);