Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组织包含多个条件的大量数据的最佳方式是什么?_C#_List_If Statement_For Loop - Fatal编程技术网

C# 组织包含多个条件的大量数据的最佳方式是什么?

C# 组织包含多个条件的大量数据的最佳方式是什么?,c#,list,if-statement,for-loop,C#,List,If Statement,For Loop,所以我的“BIGLIST”中有很多字符串,其中包含多个条件,如下所示: 颜色、国家、城镇、好/坏、白天、上午/下午/晚上/晚上 有: 5种颜色 5个国家 5镇 2好/坏 7天 4上午/下午/晚上/晚上 所以,5*5*5*2*7*2=3500个可能性 我的一些数据示例: 绿色英格兰伦敦周日晚上好 red thenetherlands amsterdam糟糕的周一晚上 蓝色美国纽约糟糕的星期二早晨 所以现在我想把每一种可能性都排成一个列表。因此,如果您在我的大列表中有2倍的可能性:蓝色美国纽约糟糕的

所以我的“BIGLIST”中有很多字符串,其中包含多个条件,如下所示: 颜色、国家、城镇、好/坏、白天、上午/下午/晚上/晚上

有:

5种颜色

5个国家

5镇

2好/坏

7天

4上午/下午/晚上/晚上

所以,5*5*5*2*7*2=3500个可能性

我的一些数据示例:

绿色英格兰伦敦周日晚上好

red thenetherlands amsterdam糟糕的周一晚上

蓝色美国纽约糟糕的星期二早晨

所以现在我想把每一种可能性都排成一个列表。因此,如果您在我的大列表中有2倍的可能性:
蓝色美国纽约糟糕的星期二早晨
,那么列表:“蓝色美国WYORKBADTUESDAYMORNINGLIST”。计数将返回2

现在,我也不想用另一个名字列清单。此外,如果我想对大名单进行排序,这是我迄今为止的想法: 这是一个好方法吗?有更简单的方法吗

List<string> colorlist = new List<string>();
colorlist[0] = "blue";
colorlist[1] = "red";
//etc
for (int i = 0;i<BIGLIST;i++)
{
   for (int j=0;j<colorlist.count;j++)
   {
       if(BIGLIST[i].contains(colorlist[j])) 
       {
          //etc
       }
   }
}
List colorlist=新列表();
颜色列表[0]=“蓝色”;
颜色列表[1]=“红色”;
//等

对于(int i=0;i我将使用自定义哈希算法实现这一点,该算法具有简单的链接,如果相同的值,则不进行探测。这将是最快的方法

您可以开始使用枚举的indexnumbers或字符串结果的长度等

只需返回List.Count()即可

如果您需要有关哈希的更多信息,请注意以下内容:

编辑:


阅读您的评论后,如果您只想生成所有排列,则应使用类别的可能值制作枚举或列表。然后将代码改编为c#从这个问题开始

您可以按如下方式组织数据:

class Program {
    static void Main(string[] args) {
        List<Criteria> list = new List<Criteria>() {
            new Criteria(Color.green, Country.england, Town.london, GoodBad.good, DayOfTheWeek.sunday, Daytime.evening),
            new Criteria(Color.red, Country.thenetherlands, Town.amsterdam, GoodBad.bad, DayOfTheWeek.monday, Daytime.night),
            new Criteria(Color.blue, Country.america, Town.newyork, GoodBad.bad, DayOfTheWeek.tuesday, Daytime.morning),
        };


        Console.WriteLine("- Native sorting:");
        list.Sort();
        foreach(var criteria in list) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Color:");
        IOrderedEnumerable<Criteria> byColor = list.OrderBy(c => c.Color);
        foreach(var criteria in byColor) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Country:");
        IOrderedEnumerable<Criteria> byCountry = list.OrderBy(c => c.Country);
        foreach(var criteria in byCountry) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Town:");
        IOrderedEnumerable<Criteria> byTown = list.OrderBy(c => c.Town);
        foreach(var criteria in byTown) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Good:");
        IOrderedEnumerable<Criteria> byGood = list.OrderBy(c => c.GoodBad);
        foreach(var criteria in byGood) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By DayOfTheWeek:");
        IOrderedEnumerable<Criteria> byDayOfTheWeek = list.OrderBy(c => c.DayOfTheWeek);
        foreach(var criteria in byDayOfTheWeek) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Daytime:");
        IOrderedEnumerable<Criteria> byDaytime = list.OrderBy(c => c.Daytime);
        foreach(var criteria in byDaytime) {
            Console.WriteLine(criteria);
        }

        Console.ReadKey();
    }
}

sealed class Criteria : IComparable<Criteria> {
    public readonly Color Color;
    public readonly Country Country;
    public readonly Town Town;
    public readonly GoodBad GoodBad;
    public readonly DayOfTheWeek DayOfTheWeek;
    public readonly Daytime Daytime;

    public Criteria(Color color, Country country, Town town, GoodBad goodBad, DayOfTheWeek dayOfTheWeek, Daytime daytime) {
        this.Color = color;
        this.Country = country;
        this.Town = town;
        this.GoodBad = goodBad;
        this.DayOfTheWeek = dayOfTheWeek;
        this.Daytime = daytime;
    }

    public override int GetHashCode() {
        int result = (int)Color | (int)Country << 2 | (int)Town << 5 | (int)GoodBad << 8 | (int)DayOfTheWeek << 9 | (int)Daytime << 12;
        return result;
    }

    public override string ToString() {
        return string.Join(" ", Color, Country, Town, GoodBad, DayOfTheWeek, Daytime);
    }

    public int CompareTo(Criteria that) {
        int result = this.Color.CompareTo(that.Color);
        if(result != 0) {
            return result;
        }
        result = this.Country.CompareTo(that.Country);
        if(result != 0) {
            return result;
        }
        result = this.Town.CompareTo(that.Town);
        if(result != 0) {
            return result;
        }
        result = this.GoodBad.CompareTo(that.GoodBad);
        if(result != 0) {
            return result;
        }
        result = this.DayOfTheWeek.CompareTo(that.DayOfTheWeek);
        if(result != 0) {
            return result;
        }
        result = this.Daytime.CompareTo(that.Daytime);
        return result;
    }
}

//2 bits
enum Color {
    green,
    red,
    blue,
}

//3 bits
enum Country {
    england,
    thenetherlands,
    america,
}

//3 bits
enum Town {
    london,
    amsterdam,
    newyork,
}

//1 bit
enum GoodBad {
    good,
    bad,
}

//3 bits
enum DayOfTheWeek {
    monday,
    tuesday,
    wednesday,
    thursday,
    friday,
    saturday,
    sunday,
}

//3 bits
enum Daytime {
    morning,
    afternoon,
    evening,
    night,
}
类程序{
静态void Main(字符串[]参数){
列表=新列表(){
新标准(颜色、绿色、乡村、英格兰、城镇、伦敦、GoodBad.good、星期天、白天、晚上),
新标准(颜色、红色、乡村、以太兰、城镇、阿姆斯特丹、GoodBad.bad、星期一、白天、晚上),
新标准(颜色、蓝色、乡村、美国、城镇、纽约、古德巴德、巴德、星期二、白天、上午),
};
Console.WriteLine(“-Native sorting:”);
list.Sort();
foreach(列表中的var标准){
控制台写入线(标准);
}
Console.WriteLine();
控制台。WriteLine(“-按颜色:”);
IOrderedEnumerable byColor=list.OrderBy(c=>c.Color);
foreach(按颜色表示的var标准){
控制台写入线(标准);
}
Console.WriteLine();
Console.WriteLine(“-按国家:”);
IOrderedEnumerable byCountry=list.OrderBy(c=>c.Country);
foreach(按国家/地区划分的风险值标准){
控制台写入线(标准);
}
Console.WriteLine();
控制台。WriteLine(“-By-Town:”);
IOrderedEnumerable byTown=list.OrderBy(c=>c.Town);
foreach(byTown的var标准){
控制台写入线(标准);
}
Console.WriteLine();
控制台。书写线(“-By-Good:”);
IOrderedEnumerable byGood=list.OrderBy(c=>c.GoodBad);
foreach(byGood中的var标准){
控制台写入线(标准);
}
Console.WriteLine();
控制台。WriteLine(“-By-dayofweek:”);
IOrderedEnumerable ByDayOfWeek=list.OrderBy(c=>c.DayOfWeek);
foreach(星期二的var标准){
控制台写入线(标准);
}
Console.WriteLine();
控制台写线(“-白天:”);
IOrderedEnumerable ByDaily=list.OrderBy(c=>c.daily);
foreach(日间的var标准){
控制台写入线(标准);
}
Console.ReadKey();
}
}
密封类标准:i可比较{
公共只读颜色;
公共只读国家/地区;
公共只读城镇;
公共只读GoodBad GoodBad;
公共只读星期一星期一星期二;
公众日间只读;
公共标准(颜色、乡村、城镇、GoodBad GoodBad、DayOfWeek DayOfWeek、日间){
这个。颜色=颜色;
这个国家=国家;
这个。镇=镇;
this.GoodBad=GoodBad;
this.dayOfWeek=DayOfWeek;
这个。白天=白天;
}
公共覆盖int GetHashCode(){

int result=(int)Color |(int)Country不确定是否理解:为什么不创建一个属性为“Color”、“Country”、“town”等的类?
2
是您想要实现的结果?
List colorlist=new List();colorlist[0]=“蓝色”;colorlist[1]=“红色”
这行不通……它不是Javascript..你应该添加项。colorlist.add(“蓝色”);@Jeroen van Langen:
List colorlist=new List(){“蓝色”,“红色”};
有效(请参见)@binkassaryman Yep,这是另一个解决方案。。