我编写了这段代码来生成两个项目集。
我正在编写list<list<>>
列表列表以生成两个项目集,并希望优化代码以生成长度为 k 的项目集。
using System.Data.OleDb;
using Microsoft.AnalysisServices.AdomdClient;
namespace HW3
{
class Program
{
private static int Min_support = 360;
private static List<String> itemSet = new List<string>();
private static List<List<String>> C_itemSet = new List<List<string>>();
static void Main(string[] args)
{
List_Movies();
//for (int k = 1; k < 3; k++)
//{ }
C_itemSet = Geneate_Sets(itemSet);
for (int i = 0; i < C_itemSet.Count; i++)
{
for (int j = 0; j < C_itemSet[i].Count; j++)
{
Console.Write(C_itemSet.ElementAt(i).ElementAt(j));
}
Console.WriteLine();
}
Console.ReadKey();
}
private static void List_Movies()
{
string connectionString = @"Provider=SQLOLEDB;Server=.;AttachDbFilename=D:\DBs\MovieClick.mdf;Database=dbname; Trusted_Connection=Yes;";
OleDbConnection myConn = new OleDbConnection(connectionString);
myConn.Open();
string str = "select Movie,COUNT(Movie) from Movies group by Movie HAVING COUNT(MOVIE) > "+Min_support;
OleDbCommand myComm = new OleDbCommand(str, myConn);
OleDbDataReader DR = myComm.ExecuteReader();
while (DR.Read())
{
if (DR[0] != null)
itemSet.Add(DR[0].ToString()); // Movie name
}
myConn.Close();
}
private static List<List<String>> Geneate_Sets(List<String> fSet )
{
List<List<String>> finalSet= new List<List<string>>();
for (int i = 0; i < fSet.Count; i++)
{
for (int j = i+1; j < fSet.Count; j++)
{
if (!fSet[i].Equals(fSet[j]))
{
List<string> temp = new List<string>();
temp.Add(fSet[i]);
temp.Add(fSet[j]);
finalSet.Add(temp);
}
}
}
return finalSet;
}