1

我正在构建的 ASP.NET 网站上有一个功能,允许用户在特定事件中注册多个类别。站点管理员需要能够添加这些类别,并且还需要添加规则来管理哪些类别组吸引折扣。

我的解决方案是创建一个名为 CategorySelectionRule 的表头,如下所示:

CREATE TABLE CategorySelectionRules (
RuleId INTEGER IDENTITY PRIMARY KEY,
RuleDescription VARCHAR(100),
RuleMessage VARCHAR(255),
Discount DECIMAL
)

...和一个名为 CategorySelectionRuleDetail 的详细信息表,如下所示:

CREATE TABLE CategorySelectionRuleDetail (
RuleCategoryId INTEGER IDENTITY PRIMARY KEY,
RuleId INTEGER REFERENCES CategorySelectionRules(RuleId),
CategoryId INTEGER REFERENCES EntryCategory(CategoryId)
}

因此,如果有人输入类别 1 和 4,代码会检查包含这些类别(并且仅包含这些类别)的规则,然后返回适用的折扣和消息。我的问题是代码失控了,因为我需要在规则中允许任意数量的类别,但是如果没有将详细信息表重新连接到自身的选择案例,我无法弄清楚如何做到这一点,像这样,它只适用于具有两个类别的规则(哦,它实际上不起作用):

categoryRuleId = (From r1 In _db.CategorySelectionRuleDetail _
                  Join r2In _db.CategorySelectionRuleDetail _
                    On r1.RuleId Equals r2.RuleId _
                  Where ((r1.CategoryId = matchCategory1 _
                  AndAlso r2.CategoryId = matchCategory2) _
                  OrElse (r1.CategoryId = matchCategory2 _
                  AndAlso r2.CategoryId = matchCategory1)) _
                  AndAlso !(From r3 In _db.CategorySelectionRuleDetail  _
                            Group r3 By r3.RuleId Into g_
                            Where g.Count(RuleId) > 2).Contains(r1.RuleId) _
                  Select r1.RuleId).FirstOrDefault()

有没有更好的方法来做到这一点,我没有看到?我觉得数据结构是正确的,但我确信有一种更简单的方法可以匹配我想要的数据,而无需为每个类别数量使用单独的 LINQ 语句。

4

1 回答 1

0

这个 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;

如果有两个具有相同类别要求的不同规则,这也会传回多个规则。

完整程序:

用于生成数据库结构的 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();
      }
    }
  }
}
于 2012-10-02T17:07:00.493 回答