简体   繁体   English

创建以逗号分隔的字符串行

[英]Create string line comma separated

I have 6 bool "categories": 我有6个布尔“类别”:

Category0, Category1, Category2, Category3, Category4, Category5.

I also have a "String ListCat" variable. 我也有一个“ String ListCat”变量。 This variable must look like something like: 此变量必须类似于:

ListCat = "0,1,2,3,4,5"

Where "0" is displayed if Category0 = true, "1" if category1 = true... 如果Category0 = true,则显示“ 0”,如果category1 = true,则显示“ 1”。

For exemple : 举个例子 :

Categorie0 = true;
Catgorie1 = true;
Categorie5 = true;

Then ListCat would be like : 那么ListCat就像:

ListCat ="0,1,5"

I have to do this to complete this query : 我必须这样做才能完成以下查询:

string StSQL = @"SELECT [Type Jour] FROM CodificationTypesJour where Categorie IN (" + ListCat + ");

How can I do that, with the commas included? 加上逗号,我该怎么办?

Thanks in advance. 提前致谢。

You can create an array of values you want to add to result, then join them using String.Join Method . 您可以创建要添加到结果中的值的数组,然后使用String.Join Method将它们连接起来。

For example if you have: 例如,如果您有:

bool category0 = true,
     category1 = true,
     category2 = false,
     category3 = false,
     category4 = false,
     category5 = true;

then you can create an array: 然后您可以创建一个数组:

string[] values = 
{
    category0 ? "0" : null,
    category1 ? "1" : null,
    category2 ? "2" : null,
    category3 ? "3" : null,
    category4 ? "4" : null,
    category5 ? "5" : null
};

and the result will be: 结果将是:

var result = string.Join(",", values.Where(s => s != null));

// output: "0,1,5"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM